1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <vcl/svapp.hxx>
30 #include <osl/mutex.hxx>
31 #include <rtl/instance.hxx>
33 #include <editeng/eeitem.hxx>
34 #include <editeng/flditem.hxx>
35 #include <editeng/unofield.hxx>
36 #include <editeng/unotext.hxx>
37 #include <comphelper/serviceinfohelper.hxx>
38 #include <comphelper/servicehelper.hxx>
40 using namespace ::rtl
;
41 using namespace ::cppu
;
42 using namespace ::com::sun::star
;
44 // ====================================================================
45 // SvxUnoTextContentEnumeration
46 // ====================================================================
48 SvxUnoTextContentEnumeration::SvxUnoTextContentEnumeration( const SvxUnoTextBase
& _rText
) throw()
51 mxParentText
= const_cast<SvxUnoTextBase
*>(&_rText
);
52 if( mrText
.GetEditSource() )
53 mpEditSource
= mrText
.GetEditSource()->Clone();
59 SvxUnoTextContentEnumeration::~SvxUnoTextContentEnumeration() throw()
64 // container::XEnumeration
65 sal_Bool SAL_CALL
SvxUnoTextContentEnumeration::hasMoreElements(void)
66 throw( uno::RuntimeException
)
68 SolarMutexGuard aGuard
;
69 if( mpEditSource
&& mpEditSource
->GetTextForwarder() )
70 return mnNextParagraph
< mpEditSource
->GetTextForwarder()->GetParagraphCount();
75 uno::Any
SvxUnoTextContentEnumeration::nextElement(void) throw( container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
77 SolarMutexGuard aGuard
;
79 if(!hasMoreElements())
80 throw container::NoSuchElementException();
82 SvxUnoTextContent
* pContent
= 0;
84 const SvxUnoTextRangeBaseList
& rRanges( mpEditSource
->getRanges() );
85 SvxUnoTextRangeBaseList::const_iterator aIter
;
86 for( aIter
= rRanges
.begin(); (aIter
!= rRanges
.end()) && (pContent
== 0); ++aIter
)
88 SvxUnoTextContent
* pIterContent
= dynamic_cast< SvxUnoTextContent
* >( (*aIter
) );
89 if( pIterContent
&& (pIterContent
->mnParagraph
== mnNextParagraph
) )
90 pContent
= pIterContent
;
94 pContent
= new SvxUnoTextContent( mrText
, mnNextParagraph
);
98 uno::Reference
< text::XTextContent
> xRef( pContent
);
99 return uno::makeAny( xRef
);
102 // ====================================================================
103 // class SvxUnoTextContent
104 // ====================================================================
106 SvxUnoTextContent::SvxUnoTextContent( const SvxUnoTextBase
& rText
, sal_uInt16 nPara
) throw()
107 : SvxUnoTextContent_Base(rText
)
109 , mrParentText(rText
)
110 , maDisposeListeners(maDisposeContainerMutex
)
111 , mbDisposing( false )
113 mxParentText
= const_cast<SvxUnoTextBase
*>(&rText
);
114 if( GetEditSource() && GetEditSource()->GetTextForwarder() )
115 SetSelection( ESelection( mnParagraph
,0, mnParagraph
, GetEditSource()->GetTextForwarder()->GetTextLen( mnParagraph
) ) );
118 SvxUnoTextContent::SvxUnoTextContent( const SvxUnoTextContent
& rContent
) throw()
119 : SvxUnoTextContent_Base(rContent
)
120 , mrParentText(rContent
.mrParentText
)
121 , maDisposeListeners(maDisposeContainerMutex
)
122 , mbDisposing( false )
124 mxParentText
= rContent
.mxParentText
;
125 mnParagraph
= rContent
.mnParagraph
;
126 SetSelection( rContent
.GetSelection() );
129 SvxUnoTextContent::~SvxUnoTextContent() throw()
135 uno::Reference
< text::XText
> SAL_CALL
SvxUnoTextContent::getText()
136 throw(uno::RuntimeException
)
141 // text::XTextContent
142 void SAL_CALL
SvxUnoTextContent::attach( const uno::Reference
< text::XTextRange
>& )
143 throw(lang::IllegalArgumentException
, uno::RuntimeException
)
147 uno::Reference
< text::XTextRange
> SAL_CALL
SvxUnoTextContent::getAnchor() throw( uno::RuntimeException
)
149 return uno::Reference
< text::XTextRange
>::query( mxParentText
);
154 void SAL_CALL
SvxUnoTextContent::dispose()
155 throw(uno::RuntimeException
)
157 SolarMutexGuard aGuard
;
160 return; // catched a recursion
164 lang::EventObject aEvt
;
165 aEvt
.Source
= *(OWeakAggObject
*) this;
166 maDisposeListeners
.disposeAndClear(aEvt
);
168 if( mxParentText
.is() )
169 mxParentText
->removeTextContent( this );
172 void SAL_CALL
SvxUnoTextContent::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
173 throw(uno::RuntimeException
)
175 maDisposeListeners
.addInterface(xListener
);
178 void SAL_CALL
SvxUnoTextContent::removeEventListener( const uno::Reference
< lang::XEventListener
>& aListener
)
179 throw(uno::RuntimeException
)
181 maDisposeListeners
.removeInterface(aListener
);
184 // XEnumerationAccess
186 uno::Reference
< container::XEnumeration
> SAL_CALL
SvxUnoTextContent::createEnumeration( )
187 throw(uno::RuntimeException
)
189 SolarMutexGuard aGuard
;
191 return new SvxUnoTextRangeEnumeration( mrParentText
, mnParagraph
);
194 // XElementAccess ( container::XEnumerationAccess )
196 uno::Type SAL_CALL
SvxUnoTextContent::getElementType()
197 throw(uno::RuntimeException
)
199 return ::getCppuType((const uno::Reference
< text::XTextRange
>*)0);
202 sal_Bool SAL_CALL
SvxUnoTextContent::hasElements()
203 throw(uno::RuntimeException
)
205 SolarMutexGuard aGuard
;
207 SvxTextForwarder
* pForwarder
= GetEditSource() ? GetEditSource()->GetTextForwarder() : NULL
;
210 std::vector
<sal_uInt16
> aPortions
;
211 pForwarder
->GetPortions( mnParagraph
, aPortions
);
212 return !aPortions
.empty();
222 void SAL_CALL
SvxUnoTextContent::setPropertyValue( const OUString
& aPropertyName
, const uno::Any
& aValue
)
223 throw(beans::UnknownPropertyException
, beans::PropertyVetoException
, lang::IllegalArgumentException
, lang::WrappedTargetException
, uno::RuntimeException
)
225 _setPropertyValue( aPropertyName
, aValue
, mnParagraph
);
228 uno::Any SAL_CALL
SvxUnoTextContent::getPropertyValue( const OUString
& PropertyName
)
229 throw(beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
)
231 return _getPropertyValue( PropertyName
, mnParagraph
);
235 void SAL_CALL
SvxUnoTextContent::setPropertyValues( const uno::Sequence
< ::rtl::OUString
>& aPropertyNames
, const uno::Sequence
< uno::Any
>& aValues
) throw (beans::PropertyVetoException
, lang::IllegalArgumentException
, lang::WrappedTargetException
, uno::RuntimeException
)
237 _setPropertyValues( aPropertyNames
, aValues
, mnParagraph
);
240 uno::Sequence
< uno::Any
> SAL_CALL
SvxUnoTextContent::getPropertyValues( const uno::Sequence
< ::rtl::OUString
>& aPropertyNames
) throw (uno::RuntimeException
)
242 return _getPropertyValues( aPropertyNames
, mnParagraph
);
245 /*// XTolerantMultiPropertySet
246 uno::Sequence< beans::SetPropertyTolerantFailed > SAL_CALL SvxUnoTextContent::setPropertyValuesTolerant( const uno::Sequence< ::rtl::OUString >& aPropertyNames, const uno::Sequence< uno::Any >& aValues ) throw (lang::IllegalArgumentException, uno::RuntimeException)
248 return _setPropertyValuesTolerant(aPropertyNames, aValues, mnParagraph);
251 uno::Sequence< beans::GetPropertyTolerantResult > SAL_CALL SvxUnoTextContent::getPropertyValuesTolerant( const uno::Sequence< ::rtl::OUString >& aPropertyNames ) throw (uno::RuntimeException)
253 return _getPropertyValuesTolerant(aPropertyNames, mnParagraph);
256 uno::Sequence< beans::GetDirectPropertyTolerantResult > SAL_CALL SvxUnoTextContent::getDirectPropertyValuesTolerant( const uno::Sequence< ::rtl::OUString >& aPropertyNames )
257 throw (uno::RuntimeException)
259 return _getDirectPropertyValuesTolerant(aPropertyNames, mnParagraph);
262 // beans::XPropertyState
263 beans::PropertyState SAL_CALL
SvxUnoTextContent::getPropertyState( const OUString
& PropertyName
)
264 throw(beans::UnknownPropertyException
, uno::RuntimeException
)
266 return _getPropertyState( PropertyName
, mnParagraph
);
269 uno::Sequence
< beans::PropertyState
> SAL_CALL
SvxUnoTextContent::getPropertyStates( const uno::Sequence
< OUString
>& aPropertyName
)
270 throw(beans::UnknownPropertyException
, uno::RuntimeException
)
272 return _getPropertyStates( aPropertyName
, mnParagraph
);
275 void SAL_CALL
SvxUnoTextContent::setPropertyToDefault( const OUString
& PropertyName
)
276 throw(beans::UnknownPropertyException
, uno::RuntimeException
)
278 _setPropertyToDefault( PropertyName
, mnParagraph
);
281 // lang::XServiceInfo
283 OUString SAL_CALL
SvxUnoTextContent::getImplementationName()
284 throw(uno::RuntimeException
)
286 return OUString( RTL_CONSTASCII_USTRINGPARAM("SvxUnoTextContent") );
289 uno::Sequence
< OUString
> SAL_CALL
SvxUnoTextContent::getSupportedServiceNames()
290 throw(uno::RuntimeException
)
292 uno::Sequence
< OUString
> aSeq( SvxUnoTextRangeBase::getSupportedServiceNames() );
293 comphelper::ServiceInfoHelper::addToSequence( aSeq
, 5, "com.sun.star.style.ParagraphProperties",
294 "com.sun.star.style.ParagraphPropertiesComplex",
295 "com.sun.star.style.ParagraphPropertiesAsian",
296 "com.sun.star.text.TextContent",
297 "com.sun.star.text.Paragraph");
301 // ====================================================================
302 // class SvxUnoTextRangeEnumeration
303 // ====================================================================
305 SvxUnoTextRangeEnumeration::SvxUnoTextRangeEnumeration( const SvxUnoTextBase
& rText
, sal_uInt16 nPara
) throw()
306 : mxParentText( const_cast<SvxUnoTextBase
*>(&rText
) ),
307 mrParentText( rText
),
308 mnParagraph( nPara
),
311 mpEditSource
= rText
.GetEditSource() ? rText
.GetEditSource()->Clone() : NULL
;
313 if( mpEditSource
&& mpEditSource
->GetTextForwarder() )
315 mpPortions
= new std::vector
<sal_uInt16
>;
316 mpEditSource
->GetTextForwarder()->GetPortions( nPara
, *mpPortions
);
324 SvxUnoTextRangeEnumeration::~SvxUnoTextRangeEnumeration() throw()
330 // container::XEnumeration
332 sal_Bool SAL_CALL
SvxUnoTextRangeEnumeration::hasMoreElements()
333 throw(uno::RuntimeException
)
335 SolarMutexGuard aGuard
;
337 return mpPortions
&& mnNextPortion
< mpPortions
->size();
340 uno::Any SAL_CALL
SvxUnoTextRangeEnumeration::nextElement()
341 throw(container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
343 SolarMutexGuard aGuard
;
345 if( mpPortions
== NULL
|| mnNextPortion
>= mpPortions
->size() )
346 throw container::NoSuchElementException();
348 sal_uInt16 nStartPos
= 0;
349 if (mnNextPortion
> 0)
350 nStartPos
= mpPortions
->at(mnNextPortion
-1);
351 sal_uInt16 nEndPos
= mpPortions
->at(mnNextPortion
);
352 ESelection
aSel( mnParagraph
, nStartPos
, mnParagraph
, nEndPos
);
354 uno::Reference
< text::XTextRange
> xRange
;
356 const SvxUnoTextRangeBaseList
& rRanges( mpEditSource
->getRanges() );
358 SvxUnoTextRange
* pRange
= 0;
360 SvxUnoTextRangeBaseList::const_iterator aIter
;
361 for( aIter
= rRanges
.begin(); (aIter
!= rRanges
.end()) && (pRange
== 0); ++aIter
)
363 SvxUnoTextRange
* pIterRange
= dynamic_cast< SvxUnoTextRange
* >( (*aIter
) );
364 if( pIterRange
&& pIterRange
->mbPortion
&& (aSel
.IsEqual( pIterRange
->maSelection
) ) )
370 pRange
= new SvxUnoTextRange( mrParentText
, sal_True
);
371 pRange
->SetSelection(aSel
);
378 return uno::makeAny( xRange
);
381 // ====================================================================
382 // class SvxUnoTextCursor
383 // ====================================================================
385 SvxUnoTextCursor_Base::SvxUnoTextCursor_Base(SvxUnoTextCursor_Base
const & base
)
387 SvxUnoTextCursor_Base0(base
)
390 SvxUnoTextCursor_Base::SvxUnoTextCursor_Base(SvxUnoTextRangeBase
const & base
)
392 SvxUnoTextCursor_Base0(base
)
395 SvxUnoTextCursor_Base::~SvxUnoTextCursor_Base() throw () {}
397 uno::Reference
< uno::XInterface
> SvxUnoTextCursor_NewInstance()
400 uno::Reference
< text::XText
> xText( (text::XText
*)new SvxUnoTextCursor( aText
) );
401 uno::Reference
< uno::XInterface
> xInt( xText
, uno::UNO_QUERY
);
405 SvxUnoTextCursor::SvxUnoTextCursor( const SvxUnoTextBase
& rText
) throw()
406 : SvxUnoTextCursor_Base(rText
),
407 mxParentText( const_cast<SvxUnoTextBase
*>(&rText
) )
411 SvxUnoTextCursor::SvxUnoTextCursor( const SvxUnoTextCursor
& rCursor
) throw()
412 : SvxUnoTextCursor_Base(rCursor
)
413 , mxParentText(rCursor
.mxParentText
)
417 SvxUnoTextCursor::~SvxUnoTextCursor() throw()
422 void SAL_CALL
SvxUnoTextCursor::collapseToStart()
423 throw(uno::RuntimeException
)
425 SolarMutexGuard aGuard
;
429 void SAL_CALL
SvxUnoTextCursor::collapseToEnd()
430 throw(uno::RuntimeException
)
432 SolarMutexGuard aGuard
;
436 sal_Bool SAL_CALL
SvxUnoTextCursor::isCollapsed()
437 throw(uno::RuntimeException
)
439 SolarMutexGuard aGuard
;
440 return IsCollapsed();
443 sal_Bool SAL_CALL
SvxUnoTextCursor::goLeft( sal_Int16 nCount
, sal_Bool bExpand
)
444 throw(uno::RuntimeException
)
446 SolarMutexGuard aGuard
;
447 return GoLeft( nCount
, bExpand
);
450 sal_Bool SAL_CALL
SvxUnoTextCursor::goRight( sal_Int16 nCount
, sal_Bool bExpand
)
451 throw(uno::RuntimeException
)
453 SolarMutexGuard aGuard
;
454 return GoRight( nCount
, bExpand
);
457 void SAL_CALL
SvxUnoTextCursor::gotoStart( sal_Bool bExpand
)
458 throw(uno::RuntimeException
)
460 SolarMutexGuard aGuard
;
461 GotoStart( bExpand
);
464 void SAL_CALL
SvxUnoTextCursor::gotoEnd( sal_Bool bExpand
)
465 throw(uno::RuntimeException
)
467 SolarMutexGuard aGuard
;
471 void SAL_CALL
SvxUnoTextCursor::gotoRange( const uno::Reference
< text::XTextRange
>& xRange
, sal_Bool bExpand
)
472 throw(uno::RuntimeException
)
477 SvxUnoTextRangeBase
* pRange
= SvxUnoTextRangeBase::getImplementation( xRange
);
481 ESelection aNewSel
= pRange
->GetSelection();
485 const ESelection
& rOldSel
= GetSelection();
486 aNewSel
.nStartPara
= rOldSel
.nStartPara
;
487 aNewSel
.nStartPos
= rOldSel
.nStartPos
;
490 SetSelection( aNewSel
);
494 // text::XTextRange (rest in SvxTextRange)
495 uno::Reference
< text::XText
> SAL_CALL
SvxUnoTextCursor::getText(void) throw( uno::RuntimeException
)
500 uno::Reference
< text::XTextRange
> SAL_CALL
SvxUnoTextCursor::getStart()
501 throw(uno::RuntimeException
)
503 return SvxUnoTextRangeBase::getStart();
506 uno::Reference
< text::XTextRange
> SAL_CALL
SvxUnoTextCursor::getEnd()
507 throw(uno::RuntimeException
)
509 return SvxUnoTextRangeBase::getEnd();
512 OUString SAL_CALL
SvxUnoTextCursor::getString() throw( uno::RuntimeException
)
514 return SvxUnoTextRangeBase::getString();
517 void SAL_CALL
SvxUnoTextCursor::setString( const OUString
& aString
) throw(uno::RuntimeException
)
519 SvxUnoTextRangeBase::setString(aString
);
521 // lang::XServiceInfo
522 OUString SAL_CALL
SvxUnoTextCursor::getImplementationName() throw(uno::RuntimeException
)
524 return OUString(RTL_CONSTASCII_USTRINGPARAM("SvxUnoTextCursor"));
527 sal_Bool SAL_CALL
SvxUnoTextCursor::supportsService( const OUString
& ServiceName
) throw(uno::RuntimeException
)
529 return comphelper::ServiceInfoHelper::supportsService( ServiceName
, getSupportedServiceNames() );
532 uno::Sequence
< OUString
> SAL_CALL
SvxUnoTextCursor::getSupportedServiceNames() throw(uno::RuntimeException
)
534 uno::Sequence
< OUString
> aSeq( SvxUnoTextRangeBase::getSupportedServiceNames() );
535 comphelper::ServiceInfoHelper::addToSequence( aSeq
, 4,"com.sun.star.style.ParagraphProperties",
536 "com.sun.star.style.ParagraphPropertiesComplex",
537 "com.sun.star.style.ParagraphPropertiesAsian",
538 "com.sun.star.text.TextCursor");
543 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */