merge the formfield patch from ooo-build
[ooovba.git] / sc / source / ui / unoobj / nameuno.cxx
blob122e4591573cb86ece7fe535f6c88c09b20fcf5d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: nameuno.cxx,v $
10 * $Revision: 1.21.132.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 #include <svtools/smplhint.hxx>
38 #include <com/sun/star/sheet/NamedRangeFlag.hpp>
39 #include <com/sun/star/awt/XBitmap.hpp>
40 #include <com/sun/star/beans/PropertyAttribute.hpp>
42 using namespace ::com::sun::star;
45 #include "nameuno.hxx"
46 #include "miscuno.hxx"
47 #include "cellsuno.hxx"
48 #include "convuno.hxx"
49 #include "targuno.hxx"
50 #include "tokenuno.hxx"
51 #include "tokenarray.hxx"
52 #include "docsh.hxx"
53 #include "docfunc.hxx"
54 #include "rangenam.hxx"
55 //CHINA001 #include "namecrea.hxx" // NAME_TOP etc.
56 #include "unoguard.hxx"
57 #include "unonames.hxx"
59 #include "scui_def.hxx" //CHINA001
61 //------------------------------------------------------------------------
63 const SfxItemPropertyMapEntry* lcl_GetNamedRangeMap()
65 static SfxItemPropertyMapEntry aNamedRangeMap_Impl[] =
67 {MAP_CHAR_LEN(SC_UNO_LINKDISPBIT), 0, &getCppuType((uno::Reference<awt::XBitmap>*)0), beans::PropertyAttribute::READONLY, 0 },
68 {MAP_CHAR_LEN(SC_UNO_LINKDISPNAME), 0, &getCppuType((rtl::OUString*)0), beans::PropertyAttribute::READONLY, 0 },
69 {MAP_CHAR_LEN(SC_UNONAME_TOKENINDEX), 0, &getCppuType((sal_Int32*)0), beans::PropertyAttribute::READONLY, 0 },
70 {MAP_CHAR_LEN(SC_UNONAME_ISSHAREDFMLA), 0, &getBooleanCppuType(), 0, 0 },
71 {0,0,0,0,0,0}
73 return aNamedRangeMap_Impl;
76 //------------------------------------------------------------------------
78 #define SCNAMEDRANGEOBJ_SERVICE "com.sun.star.sheet.NamedRange"
80 SC_SIMPLE_SERVICE_INFO( ScLabelRangeObj, "ScLabelRangeObj", "com.sun.star.sheet.LabelRange" )
81 SC_SIMPLE_SERVICE_INFO( ScLabelRangesObj, "ScLabelRangesObj", "com.sun.star.sheet.LabelRanges" )
82 SC_SIMPLE_SERVICE_INFO( ScNamedRangesObj, "ScNamedRangesObj", "com.sun.star.sheet.NamedRanges" )
84 //------------------------------------------------------------------------
86 sal_Bool lcl_UserVisibleName( const ScRangeData* pData )
88 //! als Methode an ScRangeData
90 return ( pData && !pData->HasType( RT_DATABASE ) && !pData->HasType( RT_SHARED ) );
93 //------------------------------------------------------------------------
95 ScNamedRangeObj::ScNamedRangeObj(ScDocShell* pDocSh, const String& rNm) :
96 pDocShell( pDocSh ),
97 aName( rNm )
99 pDocShell->GetDocument()->AddUnoObject(*this);
102 ScNamedRangeObj::~ScNamedRangeObj()
104 if (pDocShell)
105 pDocShell->GetDocument()->RemoveUnoObject(*this);
108 void ScNamedRangeObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
110 // Ref-Update interessiert nicht
112 if ( rHint.ISA( SfxSimpleHint ) && ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
113 pDocShell = NULL; // ungueltig geworden
116 // Hilfsfuntionen
118 ScRangeData* ScNamedRangeObj::GetRangeData_Impl()
120 ScRangeData* pRet = NULL;
121 if (pDocShell)
123 ScRangeName* pNames = pDocShell->GetDocument()->GetRangeName();
124 if (pNames)
126 sal_uInt16 nPos = 0;
127 if (pNames->SearchName( aName, nPos ))
129 pRet = (*pNames)[nPos];
130 pRet->ValidateTabRefs(); // adjust relative tab refs to valid tables
134 return pRet;
137 // sheet::XNamedRange
139 void ScNamedRangeObj::Modify_Impl( const String* pNewName, const ScTokenArray* pNewTokens, const String* pNewContent,
140 const ScAddress* pNewPos, const sal_uInt16* pNewType,
141 const formula::FormulaGrammar::Grammar eGrammar )
143 if (pDocShell)
145 ScDocument* pDoc = pDocShell->GetDocument();
146 ScRangeName* pNames = pDoc->GetRangeName();
147 if (pNames)
149 sal_uInt16 nPos = 0;
150 if (pNames->SearchName( aName, nPos ))
152 ScRangeName* pNewRanges = new ScRangeName( *pNames );
153 ScRangeData* pOld = (*pNames)[nPos];
155 String aInsName(pOld->GetName());
156 if (pNewName)
157 aInsName = *pNewName;
158 String aContent; // Content string based =>
159 pOld->GetSymbol( aContent, eGrammar); // no problems with changed positions and such.
160 if (pNewContent)
161 aContent = *pNewContent;
162 ScAddress aPos(pOld->GetPos());
163 if (pNewPos)
164 aPos = *pNewPos;
165 sal_uInt16 nType = pOld->GetType();
166 if (pNewType)
167 nType = *pNewType;
169 ScRangeData* pNew = NULL;
170 if ( pNewTokens )
171 pNew = new ScRangeData( pDoc, aInsName, *pNewTokens, aPos, nType );
172 else
173 pNew = new ScRangeData( pDoc, aInsName, aContent, aPos, nType, eGrammar );
174 pNew->SetIndex( pOld->GetIndex() );
176 pNewRanges->AtFree( nPos );
177 if ( pNewRanges->Insert(pNew) )
179 ScDocFunc aFunc(*pDocShell);
180 aFunc.SetNewRangeNames( pNewRanges, sal_True );
182 aName = aInsName; //! broadcast?
184 else
186 delete pNew; //! uno::Exception/Fehler oder so
187 delete pNewRanges;
195 rtl::OUString SAL_CALL ScNamedRangeObj::getName() throw(uno::RuntimeException)
197 ScUnoGuard aGuard;
198 return aName;
201 void SAL_CALL ScNamedRangeObj::setName( const rtl::OUString& aNewName )
202 throw(uno::RuntimeException)
204 ScUnoGuard aGuard;
205 //! Formeln anpassen ?????
207 String aNewStr(aNewName);
208 // GRAM_PODF_A1 for API compatibility.
209 Modify_Impl( &aNewStr, NULL, NULL, NULL, NULL,formula::FormulaGrammar::GRAM_PODF_A1 );
211 if ( aName != aNewStr ) // some error occured...
212 throw uno::RuntimeException(); // no other exceptions specified
215 rtl::OUString SAL_CALL ScNamedRangeObj::getContent() throw(uno::RuntimeException)
217 ScUnoGuard aGuard;
218 String aContent;
219 ScRangeData* pData = GetRangeData_Impl();
220 if (pData)
221 // GRAM_PODF_A1 for API compatibility.
222 pData->GetSymbol( aContent,formula::FormulaGrammar::GRAM_PODF_A1);
223 return aContent;
226 void SAL_CALL ScNamedRangeObj::setContent( const rtl::OUString& aContent )
227 throw(uno::RuntimeException)
229 ScUnoGuard aGuard;
230 String aContStr(aContent);
231 // GRAM_PODF_A1 for API compatibility.
232 Modify_Impl( NULL, NULL, &aContStr, NULL, NULL,formula::FormulaGrammar::GRAM_PODF_A1 );
235 void ScNamedRangeObj::SetContentWithGrammar( const ::rtl::OUString& aContent,
236 const formula::FormulaGrammar::Grammar eGrammar )
237 throw(::com::sun::star::uno::RuntimeException)
239 String aContStr(aContent);
240 Modify_Impl( NULL, NULL, &aContStr, NULL, NULL, eGrammar );
243 table::CellAddress SAL_CALL ScNamedRangeObj::getReferencePosition()
244 throw(uno::RuntimeException)
246 ScUnoGuard aGuard;
247 ScAddress aPos;
248 ScRangeData* pData = GetRangeData_Impl();
249 if (pData)
250 aPos = pData->GetPos();
251 table::CellAddress aAddress;
252 aAddress.Column = aPos.Col();
253 aAddress.Row = aPos.Row();
254 aAddress.Sheet = aPos.Tab();
255 if (pDocShell)
257 SCTAB nDocTabs = pDocShell->GetDocument()->GetTableCount();
258 if ( aAddress.Sheet >= nDocTabs && nDocTabs > 0 )
260 // Even after ValidateTabRefs, the position can be invalid if
261 // the content points to preceding tables. The resulting string
262 // is invalid in any case, so the position is just shifted.
263 aAddress.Sheet = nDocTabs - 1;
266 return aAddress;
269 void SAL_CALL ScNamedRangeObj::setReferencePosition( const table::CellAddress& aReferencePosition )
270 throw(uno::RuntimeException)
272 ScUnoGuard aGuard;
273 ScAddress aPos( (SCCOL)aReferencePosition.Column, (SCROW)aReferencePosition.Row, aReferencePosition.Sheet );
274 // GRAM_PODF_A1 for API compatibility.
275 Modify_Impl( NULL, NULL, NULL, &aPos, NULL,formula::FormulaGrammar::GRAM_PODF_A1 );
278 sal_Int32 SAL_CALL ScNamedRangeObj::getType() throw(uno::RuntimeException)
280 ScUnoGuard aGuard;
281 sal_Int32 nType=0;
282 ScRangeData* pData = GetRangeData_Impl();
283 if (pData)
285 // do not return internal RT_* flags
286 // see property 'IsSharedFormula' for RT_SHARED
287 if ( pData->HasType(RT_CRITERIA) ) nType |= sheet::NamedRangeFlag::FILTER_CRITERIA;
288 if ( pData->HasType(RT_PRINTAREA) ) nType |= sheet::NamedRangeFlag::PRINT_AREA;
289 if ( pData->HasType(RT_COLHEADER) ) nType |= sheet::NamedRangeFlag::COLUMN_HEADER;
290 if ( pData->HasType(RT_ROWHEADER) ) nType |= sheet::NamedRangeFlag::ROW_HEADER;
292 return nType;
295 void SAL_CALL ScNamedRangeObj::setType( sal_Int32 nUnoType ) throw(uno::RuntimeException)
297 // see property 'IsSharedFormula' for RT_SHARED
298 ScUnoGuard aGuard;
299 sal_uInt16 nNewType = RT_NAME;
300 if ( nUnoType & sheet::NamedRangeFlag::FILTER_CRITERIA ) nNewType |= RT_CRITERIA;
301 if ( nUnoType & sheet::NamedRangeFlag::PRINT_AREA ) nNewType |= RT_PRINTAREA;
302 if ( nUnoType & sheet::NamedRangeFlag::COLUMN_HEADER ) nNewType |= RT_COLHEADER;
303 if ( nUnoType & sheet::NamedRangeFlag::ROW_HEADER ) nNewType |= RT_ROWHEADER;
305 // GRAM_PODF_A1 for API compatibility.
306 Modify_Impl( NULL, NULL, NULL, NULL, &nNewType,formula::FormulaGrammar::GRAM_PODF_A1 );
309 // XFormulaTokens
311 uno::Sequence<sheet::FormulaToken> SAL_CALL ScNamedRangeObj::getTokens() throw(uno::RuntimeException)
313 ScUnoGuard aGuard;
314 uno::Sequence<sheet::FormulaToken> aSequence;
315 ScRangeData* pData = GetRangeData_Impl();
316 if (pData && pDocShell)
318 ScTokenArray* pTokenArray = pData->GetCode();
319 if ( pTokenArray )
320 (void)ScTokenConversion::ConvertToTokenSequence( *pDocShell->GetDocument(), aSequence, *pTokenArray );
322 return aSequence;
325 void SAL_CALL ScNamedRangeObj::setTokens( const uno::Sequence<sheet::FormulaToken>& rTokens ) throw(uno::RuntimeException)
327 ScUnoGuard aGuard;
328 if( pDocShell )
330 ScTokenArray aTokenArray;
331 (void)ScTokenConversion::ConvertToTokenArray( *pDocShell->GetDocument(), aTokenArray, rTokens );
332 // GRAM_PODF_A1 for API compatibility.
333 Modify_Impl( NULL, &aTokenArray, NULL, NULL, NULL, formula::FormulaGrammar::GRAM_PODF_A1 );
338 // XCellRangeSource
340 uno::Reference<table::XCellRange> SAL_CALL ScNamedRangeObj::getReferredCells()
341 throw(uno::RuntimeException)
343 ScUnoGuard aGuard;
344 ScRange aRange;
345 ScRangeData* pData = GetRangeData_Impl();
346 if ( pData && pData->IsReference( aRange ) )
348 //! static Funktion um ScCellObj/ScCellRangeObj zu erzeugen am ScCellRangeObj ???
350 if ( aRange.aStart == aRange.aEnd )
351 return new ScCellObj( pDocShell, aRange.aStart );
352 else
353 return new ScCellRangeObj( pDocShell, aRange );
355 return NULL;
358 // beans::XPropertySet
360 uno::Reference<beans::XPropertySetInfo> SAL_CALL ScNamedRangeObj::getPropertySetInfo()
361 throw(uno::RuntimeException)
363 ScUnoGuard aGuard;
364 static uno::Reference< beans::XPropertySetInfo > aRef(new SfxItemPropertySetInfo( lcl_GetNamedRangeMap() ));
365 return aRef;
368 void SAL_CALL ScNamedRangeObj::setPropertyValue(
369 const rtl::OUString& rPropertyName, const uno::Any& aValue )
370 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
371 lang::IllegalArgumentException, lang::WrappedTargetException,
372 uno::RuntimeException)
374 ScUnoGuard aGuard;
375 if ( rPropertyName.equalsAscii( SC_UNONAME_ISSHAREDFMLA ) )
377 bool bIsShared = false;
378 if( aValue >>= bIsShared )
380 sal_uInt16 nNewType = bIsShared ? RT_SHARED : RT_NAME;
381 Modify_Impl( NULL, NULL, NULL, NULL, &nNewType,formula::FormulaGrammar::GRAM_PODF_A1 );
386 uno::Any SAL_CALL ScNamedRangeObj::getPropertyValue( const rtl::OUString& rPropertyName )
387 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
388 uno::RuntimeException)
390 ScUnoGuard aGuard;
391 uno::Any aRet;
392 if ( rPropertyName.equalsAscii( SC_UNO_LINKDISPBIT ) )
394 // no target bitmaps for individual entries (would be all equal)
395 // ScLinkTargetTypeObj::SetLinkTargetBitmap( aRet, SC_LINKTARGETTYPE_RANGENAME );
397 else if ( rPropertyName.equalsAscii( SC_UNO_LINKDISPNAME ) )
398 aRet <<= rtl::OUString( aName );
399 else if ( rPropertyName.equalsAscii( SC_UNONAME_TOKENINDEX ) )
401 // get index for use in formula tokens (read-only)
402 ScRangeData* pData = GetRangeData_Impl();
403 if (pData)
404 aRet <<= static_cast<sal_Int32>(pData->GetIndex());
406 else if ( rPropertyName.equalsAscii( SC_UNONAME_ISSHAREDFMLA ) )
408 if( ScRangeData* pData = GetRangeData_Impl() )
409 aRet <<= static_cast< bool >( pData->HasType( RT_SHARED ) );
411 return aRet;
414 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScNamedRangeObj )
416 // lang::XServiceInfo
418 rtl::OUString SAL_CALL ScNamedRangeObj::getImplementationName() throw(uno::RuntimeException)
420 return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ScNamedRangeObj" ) );
423 sal_Bool SAL_CALL ScNamedRangeObj::supportsService( const rtl::OUString& rServiceName )
424 throw(uno::RuntimeException)
426 return rServiceName.equalsAscii( SCNAMEDRANGEOBJ_SERVICE ) ||
427 rServiceName.equalsAscii( SCLINKTARGET_SERVICE );
430 uno::Sequence<rtl::OUString> SAL_CALL ScNamedRangeObj::getSupportedServiceNames()
431 throw(uno::RuntimeException)
433 uno::Sequence<rtl::OUString> aRet(2);
434 aRet[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SCNAMEDRANGEOBJ_SERVICE ) );
435 aRet[1] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SCLINKTARGET_SERVICE ) );
436 return aRet;
440 // XUnoTunnel
442 sal_Int64 SAL_CALL ScNamedRangeObj::getSomething(
443 const uno::Sequence<sal_Int8 >& rId ) throw(uno::RuntimeException)
445 if ( rId.getLength() == 16 &&
446 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),
447 rId.getConstArray(), 16 ) )
449 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
451 return 0;
454 // static
455 const uno::Sequence<sal_Int8>& ScNamedRangeObj::getUnoTunnelId()
457 static uno::Sequence<sal_Int8> * pSeq = 0;
458 if( !pSeq )
460 osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
461 if( !pSeq )
463 static uno::Sequence< sal_Int8 > aSeq( 16 );
464 rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0, sal_True );
465 pSeq = &aSeq;
468 return *pSeq;
471 // static
472 ScNamedRangeObj* ScNamedRangeObj::getImplementation( const uno::Reference<uno::XInterface> xObj )
474 ScNamedRangeObj* pRet = NULL;
475 uno::Reference<lang::XUnoTunnel> xUT( xObj, uno::UNO_QUERY );
476 if (xUT.is())
477 pRet = reinterpret_cast<ScNamedRangeObj*>(sal::static_int_cast<sal_IntPtr>(xUT->getSomething(getUnoTunnelId())));
478 return pRet;
481 //------------------------------------------------------------------------
483 ScNamedRangesObj::ScNamedRangesObj(ScDocShell* pDocSh) :
484 pDocShell( pDocSh )
486 pDocShell->GetDocument()->AddUnoObject(*this);
489 ScNamedRangesObj::~ScNamedRangesObj()
491 if (pDocShell)
492 pDocShell->GetDocument()->RemoveUnoObject(*this);
495 void ScNamedRangesObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
497 // Referenz-Update interessiert hier nicht
499 if ( rHint.ISA( SfxSimpleHint ) &&
500 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
502 pDocShell = NULL; // ungueltig geworden
506 // sheet::XNamedRanges
508 ScNamedRangeObj* ScNamedRangesObj::GetObjectByIndex_Impl(sal_uInt16 nIndex)
510 if (pDocShell)
512 ScRangeName* pNames = pDocShell->GetDocument()->GetRangeName();
513 if (pNames)
515 sal_uInt16 nCount = pNames->GetCount();
516 sal_uInt16 nPos = 0;
517 for (sal_uInt16 i=0; i<nCount; i++)
519 ScRangeData* pData = (*pNames)[i];
520 if (lcl_UserVisibleName(pData)) // interne weglassen
522 if ( nPos == nIndex )
523 return new ScNamedRangeObj( pDocShell, pData->GetName() );
524 ++nPos;
529 return NULL;
532 ScNamedRangeObj* ScNamedRangesObj::GetObjectByName_Impl(const rtl::OUString& aName)
534 if ( pDocShell && hasByName(aName) )
535 return new ScNamedRangeObj( pDocShell, String(aName) );
536 return NULL;
539 void SAL_CALL ScNamedRangesObj::addNewByName( const rtl::OUString& aName,
540 const rtl::OUString& aContent, const table::CellAddress& aPosition,
541 sal_Int32 nUnoType ) throw(uno::RuntimeException)
543 ScUnoGuard aGuard;
544 String aNameStr(aName);
545 String aContStr(aContent);
546 ScAddress aPos( (SCCOL)aPosition.Column, (SCROW)aPosition.Row, aPosition.Sheet );
548 sal_uInt16 nNewType = RT_NAME;
549 if ( nUnoType & sheet::NamedRangeFlag::FILTER_CRITERIA ) nNewType |= RT_CRITERIA;
550 if ( nUnoType & sheet::NamedRangeFlag::PRINT_AREA ) nNewType |= RT_PRINTAREA;
551 if ( nUnoType & sheet::NamedRangeFlag::COLUMN_HEADER ) nNewType |= RT_COLHEADER;
552 if ( nUnoType & sheet::NamedRangeFlag::ROW_HEADER ) nNewType |= RT_ROWHEADER;
554 BOOL bDone = FALSE;
555 if (pDocShell)
557 ScDocument* pDoc = pDocShell->GetDocument();
558 ScRangeName* pNames = pDoc->GetRangeName();
559 USHORT nIndex = 0;
560 if (pNames && !pNames->SearchName(aNameStr, nIndex))
562 ScRangeName* pNewRanges = new ScRangeName( *pNames );
563 // GRAM_PODF_A1 for API compatibility.
564 ScRangeData* pNew = new ScRangeData( pDoc, aNameStr, aContStr,
565 aPos, nNewType,formula::FormulaGrammar::GRAM_PODF_A1 );
566 if ( pNewRanges->Insert(pNew) )
568 ScDocFunc aFunc(*pDocShell);
569 aFunc.SetNewRangeNames( pNewRanges, sal_True );
570 bDone = TRUE;
572 else
574 delete pNew;
575 delete pNewRanges;
580 if (!bDone)
581 throw uno::RuntimeException(); // no other exceptions specified
584 void SAL_CALL ScNamedRangesObj::addNewFromTitles( const table::CellRangeAddress& aSource,
585 sheet::Border aBorder ) throw(uno::RuntimeException)
587 ScUnoGuard aGuard;
588 //! das darf kein enum sein, weil mehrere Bits gesetzt sein koennen !!!
590 sal_Bool bTop = ( aBorder == sheet::Border_TOP );
591 sal_Bool bLeft = ( aBorder == sheet::Border_LEFT );
592 sal_Bool bBottom = ( aBorder == sheet::Border_BOTTOM );
593 sal_Bool bRight = ( aBorder == sheet::Border_RIGHT );
595 ScRange aRange;
596 ScUnoConversion::FillScRange( aRange, aSource );
598 sal_uInt16 nFlags = 0;
599 if (bTop) nFlags |= NAME_TOP;
600 if (bLeft) nFlags |= NAME_LEFT;
601 if (bBottom) nFlags |= NAME_BOTTOM;
602 if (bRight) nFlags |= NAME_RIGHT;
604 if (nFlags)
606 ScDocFunc aFunc(*pDocShell);
607 aFunc.CreateNames( aRange, nFlags, sal_True );
611 void SAL_CALL ScNamedRangesObj::removeByName( const rtl::OUString& aName )
612 throw(uno::RuntimeException)
614 ScUnoGuard aGuard;
615 BOOL bDone = FALSE;
616 if (pDocShell)
618 ScRangeName* pNames = pDocShell->GetDocument()->GetRangeName();
619 if (pNames)
621 String aString(aName);
622 sal_uInt16 nPos = 0;
623 if (pNames->SearchName( aString, nPos ))
624 if ( lcl_UserVisibleName((*pNames)[nPos]) )
626 ScRangeName* pNewRanges = new ScRangeName(*pNames);
627 pNewRanges->AtFree(nPos);
628 ScDocFunc aFunc(*pDocShell);
629 aFunc.SetNewRangeNames( pNewRanges, sal_True );
630 bDone = TRUE;
635 if (!bDone)
636 throw uno::RuntimeException(); // no other exceptions specified
639 void SAL_CALL ScNamedRangesObj::outputList( const table::CellAddress& aOutputPosition )
640 throw(uno::RuntimeException)
642 ScUnoGuard aGuard;
643 ScAddress aPos( (SCCOL)aOutputPosition.Column, (SCROW)aOutputPosition.Row, aOutputPosition.Sheet );
644 if (pDocShell)
646 ScDocFunc aFunc(*pDocShell);
647 aFunc.InsertNameList( aPos, sal_True );
651 // container::XEnumerationAccess
653 uno::Reference<container::XEnumeration> SAL_CALL ScNamedRangesObj::createEnumeration()
654 throw(uno::RuntimeException)
656 ScUnoGuard aGuard;
657 return new ScIndexEnumeration(this, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sheet.NamedRangesEnumeration")));
660 // container::XIndexAccess
662 sal_Int32 SAL_CALL ScNamedRangesObj::getCount() throw(uno::RuntimeException)
664 ScUnoGuard aGuard;
665 long nRet = 0;
666 if (pDocShell)
668 ScRangeName* pNames = pDocShell->GetDocument()->GetRangeName();
669 if (pNames)
671 sal_uInt16 nCount = pNames->GetCount();
672 for (sal_uInt16 i=0; i<nCount; i++)
673 if (lcl_UserVisibleName( (*pNames)[i] )) // interne weglassen
674 ++nRet;
677 return nRet;
680 uno::Any SAL_CALL ScNamedRangesObj::getByIndex( sal_Int32 nIndex )
681 throw(lang::IndexOutOfBoundsException,
682 lang::WrappedTargetException, uno::RuntimeException)
684 ScUnoGuard aGuard;
685 uno::Reference< sheet::XNamedRange > xRange(GetObjectByIndex_Impl((sal_uInt16)nIndex));
686 if ( xRange.is() )
687 return uno::makeAny(xRange);
688 else
689 throw lang::IndexOutOfBoundsException();
690 // return uno::Any();
693 uno::Type SAL_CALL ScNamedRangesObj::getElementType() throw(uno::RuntimeException)
695 ScUnoGuard aGuard;
696 return ::getCppuType((const uno::Reference< sheet::XNamedRange >*)0); // muss zu getByIndex passen
699 sal_Bool SAL_CALL ScNamedRangesObj::hasElements() throw(uno::RuntimeException)
701 ScUnoGuard aGuard;
702 return ( getCount() != 0 );
705 uno::Any SAL_CALL ScNamedRangesObj::getByName( const rtl::OUString& aName )
706 throw(container::NoSuchElementException,
707 lang::WrappedTargetException, uno::RuntimeException)
709 ScUnoGuard aGuard;
710 uno::Reference< sheet::XNamedRange > xRange(GetObjectByName_Impl(aName));
711 if ( xRange.is() )
712 return uno::makeAny(xRange);
713 else
714 throw container::NoSuchElementException();
715 // return uno::Any();
718 uno::Sequence<rtl::OUString> SAL_CALL ScNamedRangesObj::getElementNames()
719 throw(uno::RuntimeException)
721 ScUnoGuard aGuard;
722 if (pDocShell)
724 ScRangeName* pNames = pDocShell->GetDocument()->GetRangeName();
725 if (pNames)
727 long nVisCount = getCount(); // Namen mit lcl_UserVisibleName
728 uno::Sequence<rtl::OUString> aSeq(nVisCount);
729 rtl::OUString* pAry = aSeq.getArray();
731 sal_uInt16 nCount = pNames->GetCount();
732 sal_uInt16 nVisPos = 0;
733 for (sal_uInt16 i=0; i<nCount; i++)
735 ScRangeData* pData = (*pNames)[i];
736 if ( lcl_UserVisibleName(pData) )
737 pAry[nVisPos++] = pData->GetName();
739 // DBG_ASSERT(nVisPos == nVisCount, "huch, verzaehlt?");
740 return aSeq;
743 return uno::Sequence<rtl::OUString>(0);
746 sal_Bool SAL_CALL ScNamedRangesObj::hasByName( const rtl::OUString& aName )
747 throw(uno::RuntimeException)
749 ScUnoGuard aGuard;
750 if (pDocShell)
752 ScRangeName* pNames = pDocShell->GetDocument()->GetRangeName();
753 if (pNames)
755 sal_uInt16 nPos = 0;
756 if (pNames->SearchName( String(aName), nPos ))
757 if ( lcl_UserVisibleName((*pNames)[nPos]) )
758 return sal_True;
761 return sal_False;
764 /** called from the XActionLockable interface methods on initial locking */
765 void ScNamedRangesObj::lock()
767 pDocShell->GetDocument()->CompileNameFormula( TRUE ); // CreateFormulaString
770 /** called from the XActionLockable interface methods on final unlock */
771 void ScNamedRangesObj::unlock()
773 pDocShell->GetDocument()->CompileNameFormula( FALSE ); // CompileFormulaString
776 // document::XActionLockable
778 sal_Bool ScNamedRangesObj::isActionLocked() throw(uno::RuntimeException)
780 ScUnoGuard aGuard;
781 return pDocShell->GetDocument()->GetNamedRangesLockCount() != 0;
784 void ScNamedRangesObj::addActionLock() throw(uno::RuntimeException)
786 ScUnoGuard aGuard;
787 ScDocument* pDoc = pDocShell->GetDocument();
788 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
789 ++nLockCount;
790 if ( nLockCount == 1 )
792 lock();
794 pDoc->SetNamedRangesLockCount( nLockCount );
797 void ScNamedRangesObj::removeActionLock() throw(uno::RuntimeException)
799 ScUnoGuard aGuard;
800 ScDocument* pDoc = pDocShell->GetDocument();
801 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
802 if ( nLockCount > 0 )
804 --nLockCount;
805 if ( nLockCount == 0 )
807 unlock();
809 pDoc->SetNamedRangesLockCount( nLockCount );
813 void ScNamedRangesObj::setActionLocks( sal_Int16 nLock ) throw(uno::RuntimeException)
815 ScUnoGuard aGuard;
816 if ( nLock >= 0 )
818 ScDocument* pDoc = pDocShell->GetDocument();
819 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
820 if ( nLock == 0 && nLockCount > 0 )
822 unlock();
824 if ( nLock > 0 && nLockCount == 0 )
826 lock();
828 pDoc->SetNamedRangesLockCount( nLock );
832 sal_Int16 ScNamedRangesObj::resetActionLocks() throw(uno::RuntimeException)
834 ScUnoGuard aGuard;
835 ScDocument* pDoc = pDocShell->GetDocument();
836 sal_Int16 nLockCount = pDoc->GetNamedRangesLockCount();
837 if ( nLockCount > 0 )
839 unlock();
841 pDoc->SetNamedRangesLockCount( 0 );
842 return nLockCount;
845 //------------------------------------------------------------------------
847 ScLabelRangeObj::ScLabelRangeObj(ScDocShell* pDocSh, sal_Bool bCol, const ScRange& rR) :
848 pDocShell( pDocSh ),
849 bColumn( bCol ),
850 aRange( rR )
852 pDocShell->GetDocument()->AddUnoObject(*this);
855 ScLabelRangeObj::~ScLabelRangeObj()
857 if (pDocShell)
858 pDocShell->GetDocument()->RemoveUnoObject(*this);
861 void ScLabelRangeObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
863 //! Ref-Update !!!
865 if ( rHint.ISA( SfxSimpleHint ) && ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
866 pDocShell = NULL; // ungueltig geworden
869 // Hilfsfuntionen
871 ScRangePair* ScLabelRangeObj::GetData_Impl()
873 ScRangePair* pRet = NULL;
874 if (pDocShell)
876 ScDocument* pDoc = pDocShell->GetDocument();
877 ScRangePairList* pList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
878 if (pList)
879 pRet = pList->Find( aRange );
881 return pRet;
884 void ScLabelRangeObj::Modify_Impl( const ScRange* pLabel, const ScRange* pData )
886 if (pDocShell)
888 ScDocument* pDoc = pDocShell->GetDocument();
889 ScRangePairList* pOldList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
890 if (pOldList)
892 ScRangePairListRef xNewList(pOldList->Clone());
893 ScRangePair* pEntry = xNewList->Find( aRange );
894 if (pEntry)
896 xNewList->Remove( pEntry ); // nur aus der Liste entfernt, nicht geloescht
898 if ( pLabel )
899 pEntry->GetRange(0) = *pLabel;
900 if ( pData )
901 pEntry->GetRange(1) = *pData;
903 xNewList->Join( *pEntry );
904 delete pEntry;
906 if (bColumn)
907 pDoc->GetColNameRangesRef() = xNewList;
908 else
909 pDoc->GetRowNameRangesRef() = xNewList;
911 pDoc->CompileColRowNameFormula();
912 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
913 pDocShell->SetDocumentModified();
915 //! Undo ?!?! (hier und aus Dialog)
917 if ( pLabel )
918 aRange = *pLabel; // Objekt anpassen, um Range wiederzufinden
924 // sheet::XLabelRange
926 table::CellRangeAddress SAL_CALL ScLabelRangeObj::getLabelArea()
927 throw(uno::RuntimeException)
929 ScUnoGuard aGuard;
930 table::CellRangeAddress aRet;
931 ScRangePair* pData = GetData_Impl();
932 if (pData)
933 ScUnoConversion::FillApiRange( aRet, pData->GetRange(0) );
934 return aRet;
937 void SAL_CALL ScLabelRangeObj::setLabelArea( const table::CellRangeAddress& aLabelArea )
938 throw(uno::RuntimeException)
940 ScUnoGuard aGuard;
941 ScRange aLabelRange;
942 ScUnoConversion::FillScRange( aLabelRange, aLabelArea );
943 Modify_Impl( &aLabelRange, NULL );
946 table::CellRangeAddress SAL_CALL ScLabelRangeObj::getDataArea()
947 throw(uno::RuntimeException)
949 ScUnoGuard aGuard;
950 table::CellRangeAddress aRet;
951 ScRangePair* pData = GetData_Impl();
952 if (pData)
953 ScUnoConversion::FillApiRange( aRet, pData->GetRange(1) );
954 return aRet;
957 void SAL_CALL ScLabelRangeObj::setDataArea( const table::CellRangeAddress& aDataArea )
958 throw(uno::RuntimeException)
960 ScUnoGuard aGuard;
961 ScRange aDataRange;
962 ScUnoConversion::FillScRange( aDataRange, aDataArea );
963 Modify_Impl( NULL, &aDataRange );
966 //------------------------------------------------------------------------
968 ScLabelRangesObj::ScLabelRangesObj(ScDocShell* pDocSh, sal_Bool bCol) :
969 pDocShell( pDocSh ),
970 bColumn( bCol )
972 pDocShell->GetDocument()->AddUnoObject(*this);
975 ScLabelRangesObj::~ScLabelRangesObj()
977 if (pDocShell)
978 pDocShell->GetDocument()->RemoveUnoObject(*this);
981 void ScLabelRangesObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
983 // Referenz-Update interessiert hier nicht
985 if ( rHint.ISA( SfxSimpleHint ) &&
986 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
988 pDocShell = NULL; // ungueltig geworden
992 // sheet::XLabelRanges
994 ScLabelRangeObj* ScLabelRangesObj::GetObjectByIndex_Impl(sal_uInt16 nIndex)
996 if (pDocShell)
998 ScDocument* pDoc = pDocShell->GetDocument();
999 ScRangePairList* pList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1000 if ( pList && nIndex < pList->Count() )
1002 ScRangePair* pData = pList->GetObject(nIndex);
1003 if (pData)
1004 return new ScLabelRangeObj( pDocShell, bColumn, pData->GetRange(0) );
1007 return NULL;
1010 void SAL_CALL ScLabelRangesObj::addNew( const table::CellRangeAddress& aLabelArea,
1011 const table::CellRangeAddress& aDataArea )
1012 throw(uno::RuntimeException)
1014 ScUnoGuard aGuard;
1015 if (pDocShell)
1017 ScDocument* pDoc = pDocShell->GetDocument();
1018 ScRangePairList* pOldList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1019 if (pOldList)
1021 ScRangePairListRef xNewList(pOldList->Clone());
1023 ScRange aLabelRange;
1024 ScRange aDataRange;
1025 ScUnoConversion::FillScRange( aLabelRange, aLabelArea );
1026 ScUnoConversion::FillScRange( aDataRange, aDataArea );
1027 xNewList->Join( ScRangePair( aLabelRange, aDataRange ) );
1029 if (bColumn)
1030 pDoc->GetColNameRangesRef() = xNewList;
1031 else
1032 pDoc->GetRowNameRangesRef() = xNewList;
1034 pDoc->CompileColRowNameFormula();
1035 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
1036 pDocShell->SetDocumentModified();
1038 //! Undo ?!?! (hier und aus Dialog)
1043 void SAL_CALL ScLabelRangesObj::removeByIndex( sal_Int32 nIndex )
1044 throw(uno::RuntimeException)
1046 ScUnoGuard aGuard;
1047 BOOL bDone = FALSE;
1048 if (pDocShell)
1050 ScDocument* pDoc = pDocShell->GetDocument();
1051 ScRangePairList* pOldList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1053 if ( pOldList && nIndex >= 0 && nIndex < (sal_Int32)pOldList->Count() )
1055 ScRangePairListRef xNewList(pOldList->Clone());
1057 ScRangePair* pEntry = xNewList->GetObject( nIndex );
1058 if (pEntry)
1060 xNewList->Remove( pEntry );
1061 delete pEntry;
1063 if (bColumn)
1064 pDoc->GetColNameRangesRef() = xNewList;
1065 else
1066 pDoc->GetRowNameRangesRef() = xNewList;
1068 pDoc->CompileColRowNameFormula();
1069 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
1070 pDocShell->SetDocumentModified();
1071 bDone = TRUE;
1073 //! Undo ?!?! (hier und aus Dialog)
1077 if (!bDone)
1078 throw uno::RuntimeException(); // no other exceptions specified
1081 // container::XEnumerationAccess
1083 uno::Reference<container::XEnumeration> SAL_CALL ScLabelRangesObj::createEnumeration()
1084 throw(uno::RuntimeException)
1086 ScUnoGuard aGuard;
1087 return new ScIndexEnumeration(this, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sheet.LabelRangesEnumeration")));
1090 // container::XIndexAccess
1092 sal_Int32 SAL_CALL ScLabelRangesObj::getCount() throw(uno::RuntimeException)
1094 ScUnoGuard aGuard;
1095 if (pDocShell)
1097 ScDocument* pDoc = pDocShell->GetDocument();
1098 ScRangePairList* pList = bColumn ? pDoc->GetColNameRanges() : pDoc->GetRowNameRanges();
1099 if (pList)
1100 return pList->Count();
1102 return 0;
1105 uno::Any SAL_CALL ScLabelRangesObj::getByIndex( sal_Int32 nIndex )
1106 throw(lang::IndexOutOfBoundsException,
1107 lang::WrappedTargetException, uno::RuntimeException)
1109 ScUnoGuard aGuard;
1110 uno::Reference< sheet::XLabelRange > xRange(GetObjectByIndex_Impl((sal_uInt16)nIndex));
1111 if ( xRange.is() )
1112 return uno::makeAny(xRange);
1113 else
1114 throw lang::IndexOutOfBoundsException();
1115 // return uno::Any();
1118 uno::Type SAL_CALL ScLabelRangesObj::getElementType() throw(uno::RuntimeException)
1120 ScUnoGuard aGuard;
1121 return ::getCppuType((const uno::Reference< sheet::XLabelRange >*)0); // muss zu getByIndex passen
1125 sal_Bool SAL_CALL ScLabelRangesObj::hasElements() throw(uno::RuntimeException)
1127 ScUnoGuard aGuard;
1128 return ( getCount() != 0 );
1131 //------------------------------------------------------------------------