Update ooo320-m1
[ooovba.git] / sw / source / core / unocore / unoflatpara.cxx
blob68015384da53ce9a53018ba32a82e53a3537af21
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unoflatpara.cxx,v $
10 * $Revision: 1.4 $
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_sw.hxx"
34 #include <svx/unolingu.hxx>
36 #include <unoflatpara.hxx>
38 #include <vos/mutex.hxx>
39 #include <vcl/svapp.hxx>
40 #include <com/sun/star/text/TextMarkupType.hpp>
41 #include <unotextmarkup.hxx>
42 #include <ndtxt.hxx>
43 #include <doc.hxx>
44 #include <docsh.hxx>
45 #include <viewsh.hxx>
46 #include <viewimp.hxx>
47 #include <breakit.hxx>
48 #include <pam.hxx>
49 #include <unoobj.hxx>
50 #include <pagefrm.hxx>
51 #include <cntfrm.hxx>
52 #include <rootfrm.hxx>
53 #include <poolfmt.hxx>
54 #include <pagedesc.hxx>
55 #include <IGrammarContact.hxx>
56 #include <viewopt.hxx>
58 using namespace ::com::sun::star;
60 /******************************************************************************
61 * SwXFlatParagraph
62 ******************************************************************************/
64 SwXFlatParagraph::SwXFlatParagraph( SwTxtNode& rTxtNode, rtl::OUString aExpandText, const ModelToViewHelper::ConversionMap* pMap ) :
65 SwXTextMarkup( rTxtNode, pMap ),
66 maExpandText( aExpandText )
70 SwXFlatParagraph::~SwXFlatParagraph()
74 uno::Sequence< uno::Type > SwXFlatParagraph::getTypes( ) throw(uno::RuntimeException)
76 uno::Sequence< uno::Type > aTypes = SwXTextMarkup::getTypes();
77 aTypes.realloc( aTypes.getLength() + 1 );
78 aTypes[aTypes.getLength()-1] = ::getCppuType((uno::Reference< text::XFlatParagraph >*)0);
79 return aTypes;
82 uno::Sequence< sal_Int8 > SwXFlatParagraph::getImplementationId( ) throw(uno::RuntimeException)
84 vos::OGuard aGuard(Application::GetSolarMutex());
85 static uno::Sequence< sal_Int8 > aId( 16 );
86 static sal_Bool bInit = sal_False;
87 if(!bInit)
89 rtl_createUuid( (sal_uInt8 *)(aId.getArray() ), 0, sal_True );
90 bInit = sal_True;
92 return aId;
95 uno::Any SAL_CALL SwXFlatParagraph::queryInterface( const uno::Type& rType ) throw(uno::RuntimeException)
97 if ( rType == ::getCppuType((uno::Reference< text::XFlatParagraph >*)0) )
99 return uno::makeAny( uno::Reference < text::XFlatParagraph >(this) );
101 else
102 return SwXTextMarkup::queryInterface( rType );
105 void SAL_CALL SwXFlatParagraph::acquire() throw()
107 SwXTextMarkup::acquire();
110 void SAL_CALL SwXFlatParagraph::release() throw()
112 SwXTextMarkup::release();
115 const SwTxtNode* SwXFlatParagraph::getTxtNode() const
117 return mpTxtNode;
120 css::uno::Reference< css::container::XStringKeyMap > SAL_CALL SwXFlatParagraph::getMarkupInfoContainer() throw (css::uno::RuntimeException)
122 return SwXTextMarkup::getMarkupInfoContainer();
125 void SAL_CALL SwXFlatParagraph::commitTextMarkup(::sal_Int32 nType, const ::rtl::OUString & rIdentifier, ::sal_Int32 nStart, ::sal_Int32 nLength, const css::uno::Reference< css::container::XStringKeyMap > & rxMarkupInfoContainer) throw (css::uno::RuntimeException)
127 vos::OGuard aGuard(Application::GetSolarMutex());
128 SwXTextMarkup::commitTextMarkup( nType, rIdentifier, nStart, nLength, rxMarkupInfoContainer );
131 // text::XFlatParagraph:
132 ::rtl::OUString SAL_CALL SwXFlatParagraph::getText() throw (uno::RuntimeException)
134 return maExpandText;
137 // text::XFlatParagraph:
138 void SAL_CALL SwXFlatParagraph::setChecked( ::sal_Int32 nType, ::sal_Bool bVal ) throw (uno::RuntimeException)
140 vos::OGuard aGuard(Application::GetSolarMutex());
142 if ( mpTxtNode )
144 if ( text::TextMarkupType::SPELLCHECK == nType )
145 mpTxtNode->SetWrongDirty( !bVal );
146 else if ( text::TextMarkupType::SMARTTAG == nType )
147 mpTxtNode->SetSmartTagDirty( !bVal );
148 else if( text::TextMarkupType::PROOFREADING == nType )
150 mpTxtNode->SetGrammarCheckDirty( !bVal );
151 if( bVal )
152 ::finishGrammarCheck( *mpTxtNode );
157 // text::XFlatParagraph:
158 ::sal_Bool SAL_CALL SwXFlatParagraph::isChecked( ::sal_Int32 nType ) throw (uno::RuntimeException)
160 vos::OGuard aGuard(Application::GetSolarMutex());
161 if ( mpTxtNode )
163 if ( text::TextMarkupType::SPELLCHECK == nType )
164 return mpTxtNode->IsWrongDirty();
165 else if ( text::TextMarkupType::PROOFREADING == nType )
166 return mpTxtNode->IsGrammarCheckDirty();
167 else if ( text::TextMarkupType::SMARTTAG == nType )
168 return mpTxtNode->IsSmartTagDirty();
171 return sal_False;
174 // text::XFlatParagraph:
175 ::sal_Bool SAL_CALL SwXFlatParagraph::isModified() throw (uno::RuntimeException)
177 vos::OGuard aGuard(Application::GetSolarMutex());
178 return 0 == mpTxtNode;
181 // text::XFlatParagraph:
182 lang::Locale SAL_CALL SwXFlatParagraph::getLanguageOfText(::sal_Int32 nPos, ::sal_Int32 nLen)
183 throw (uno::RuntimeException, lang::IllegalArgumentException)
185 vos::OGuard aGuard(Application::GetSolarMutex());
186 if (!mpTxtNode)
187 return SvxCreateLocale( LANGUAGE_NONE );
189 const lang::Locale aLocale( SW_BREAKITER()->GetLocale( mpTxtNode->GetLang( static_cast<USHORT>(nPos), static_cast<USHORT>(nLen) ) ) );
190 return aLocale;
193 // text::XFlatParagraph:
194 lang::Locale SAL_CALL SwXFlatParagraph::getPrimaryLanguageOfText(::sal_Int32 nPos, ::sal_Int32 nLen)
195 throw (uno::RuntimeException, lang::IllegalArgumentException)
197 vos::OGuard aGuard(Application::GetSolarMutex());
199 if (!mpTxtNode)
200 return SvxCreateLocale( LANGUAGE_NONE );
202 const lang::Locale aLocale( SW_BREAKITER()->GetLocale( mpTxtNode->GetLang( static_cast<USHORT>(nPos), static_cast<USHORT>(nLen) ) ) );
203 return aLocale;
206 // text::XFlatParagraph:
207 void SAL_CALL SwXFlatParagraph::changeText(::sal_Int32 nPos, ::sal_Int32 nLen, const ::rtl::OUString & aNewText, const css::uno::Sequence< css::beans::PropertyValue > & aAttributes) throw (css::uno::RuntimeException, css::lang::IllegalArgumentException)
209 vos::OGuard aGuard(Application::GetSolarMutex());
211 if ( !mpTxtNode )
212 return;
214 SwTxtNode* pOldTxtNode = mpTxtNode;
216 SwPaM aPaM( *mpTxtNode, static_cast<USHORT>(nPos), *mpTxtNode, static_cast<USHORT>(nPos + nLen) );
218 UnoActionContext aAction( mpTxtNode->GetDoc() );
220 uno::Reference< text::XTextRange > xRange = SwXTextRange::CreateTextRangeFromPosition( mpTxtNode->GetDoc(), *aPaM.GetPoint(), aPaM.GetMark() );
221 uno::Reference< beans::XPropertySet > xPropSet( xRange, uno::UNO_QUERY );
222 if ( xPropSet.is() )
224 for ( USHORT i = 0; i < aAttributes.getLength(); ++i )
225 xPropSet->setPropertyValue( aAttributes[i].Name, aAttributes[i].Value );
228 mpTxtNode = pOldTxtNode; // setPropertyValue() modifies this. We restore the old state.
230 IDocumentContentOperations* pIDCO = mpTxtNode->getIDocumentContentOperations();
231 pIDCO->ReplaceRange( aPaM, aNewText, false );
233 mpTxtNode = 0;
236 // text::XFlatParagraph:
237 void SAL_CALL SwXFlatParagraph::changeAttributes(::sal_Int32 nPos, ::sal_Int32 nLen, const css::uno::Sequence< css::beans::PropertyValue > & aAttributes) throw (css::uno::RuntimeException, css::lang::IllegalArgumentException)
239 vos::OGuard aGuard(Application::GetSolarMutex());
241 if ( !mpTxtNode )
242 return;
244 SwPaM aPaM( *mpTxtNode, static_cast<USHORT>(nPos), *mpTxtNode, static_cast<USHORT>(nPos + nLen) );
246 UnoActionContext aAction( mpTxtNode->GetDoc() );
248 uno::Reference< text::XTextRange > xRange = SwXTextRange::CreateTextRangeFromPosition( mpTxtNode->GetDoc(), *aPaM.GetPoint(), aPaM.GetMark() );
249 uno::Reference< beans::XPropertySet > xPropSet( xRange, uno::UNO_QUERY );
250 if ( xPropSet.is() )
252 for ( USHORT i = 0; i < aAttributes.getLength(); ++i )
253 xPropSet->setPropertyValue( aAttributes[i].Name, aAttributes[i].Value );
256 mpTxtNode = 0;
259 // text::XFlatParagraph:
260 css::uno::Sequence< ::sal_Int32 > SAL_CALL SwXFlatParagraph::getLanguagePortions() throw (css::uno::RuntimeException)
262 vos::OGuard aGuard(Application::GetSolarMutex());
263 return css::uno::Sequence< ::sal_Int32>();
266 /******************************************************************************
267 * SwXFlatParagraphIterator
268 ******************************************************************************/
270 SwXFlatParagraphIterator::SwXFlatParagraphIterator( SwDoc& rDoc, sal_Int32 nType, sal_Bool bAutomatic )
271 : mpDoc( &rDoc ),
272 mnType( nType ),
273 mbAutomatic( bAutomatic ),
274 mnCurrentNode( 0 ),
275 mnStartNode( 0 ),
276 mnEndNode( rDoc.GetNodes().Count() ),
277 mbWrapped( sal_False )
279 //mnStartNode = mnCurrentNode = get node from current cursor TODO!
281 // register as listener and get notified when document is closed
282 mpDoc->GetPageDescFromPool( RES_POOLPAGE_STANDARD )->Add(this);
285 SwXFlatParagraphIterator::~SwXFlatParagraphIterator()
290 void SwXFlatParagraphIterator::Modify( SfxPoolItem *pOld, SfxPoolItem *pNew )
292 ClientModify( this, pOld, pNew );
293 // check if document gets closed...
294 if(!GetRegisteredIn())
296 vos::OGuard aGuard(Application::GetSolarMutex());
297 mpDoc = 0;
302 uno::Reference< text::XFlatParagraph > SwXFlatParagraphIterator::getFirstPara()
303 throw( uno::RuntimeException )
305 return getNextPara(); // TODO
308 uno::Reference< text::XFlatParagraph > SwXFlatParagraphIterator::getNextPara()
309 throw( uno::RuntimeException )
311 vos::OGuard aGuard(Application::GetSolarMutex());
313 uno::Reference< text::XFlatParagraph > xRet;
314 if (!mpDoc)
315 return xRet;
317 SwTxtNode* pRet = 0;
318 if ( mbAutomatic )
320 ViewShell* pViewShell = 0;
321 mpDoc->GetEditShell( &pViewShell );
323 SwPageFrm* pCurrentPage = pViewShell ? pViewShell->Imp()->GetFirstVisPage() : 0;
324 SwPageFrm* pStartPage = pCurrentPage;
325 SwPageFrm* pStopPage = 0;
327 while ( pCurrentPage != pStopPage )
329 if (mnType != text::TextMarkupType::SPELLCHECK || pCurrentPage->IsInvalidSpelling() )
331 // this method is supposed to return an empty paragraph in case Online Checking is disabled
332 if ( ( mnType == text::TextMarkupType::PROOFREADING || mnType == text::TextMarkupType::SPELLCHECK )
333 && !pViewShell->GetViewOptions()->IsOnlineSpell() )
334 return xRet;
336 // search for invalid content:
337 SwCntntFrm* pCnt = pCurrentPage->ContainsCntnt();
339 while( pCnt && pCurrentPage->IsAnLower( pCnt ) )
341 SwTxtNode* pTxtNode = dynamic_cast<SwTxtNode*>( pCnt->GetNode()->GetTxtNode() );
343 if ( pTxtNode &&
344 ((mnType == text::TextMarkupType::SPELLCHECK &&
345 pTxtNode->IsWrongDirty()) ||
346 (mnType == text::TextMarkupType::PROOFREADING &&
347 pTxtNode->IsGrammarCheckDirty())) )
349 pRet = pTxtNode;
350 break;
353 pCnt = pCnt->GetNextCntntFrm();
357 if ( pRet )
358 break;
360 // if there is no invalid text node on the current page,
361 // we validate the page
362 pCurrentPage->ValidateSpelling();
364 // proceed with next page, wrap at end of document if required:
365 pCurrentPage = static_cast<SwPageFrm*>(pCurrentPage->GetNext());
367 if ( !pCurrentPage && !pStopPage )
369 pStopPage = pStartPage;
370 pCurrentPage = static_cast<SwPageFrm*>(mpDoc->GetRootFrm()->Lower());
374 else // non-automatic checking
376 const SwNodes& rNodes = mpDoc->GetNodes();
377 const ULONG nMaxNodes = rNodes.Count();
379 while ( mnCurrentNode < mnEndNode && mnCurrentNode < nMaxNodes )
381 SwNode* pNd = rNodes[ mnCurrentNode ];
383 ++mnCurrentNode;
385 pRet = dynamic_cast<SwTxtNode*>(pNd);
386 if ( pRet )
387 break;
389 if ( mnCurrentNode == mnEndNode && !mbWrapped )
391 mnCurrentNode = 0;
392 mnEndNode = mnStartNode;
397 if ( pRet )
399 // Expand the string:
400 rtl::OUString aExpandText;
401 const ModelToViewHelper::ConversionMap* pConversionMap =
402 pRet->BuildConversionMap( aExpandText );
404 xRet = new SwXFlatParagraph( *pRet, aExpandText, pConversionMap );
405 // keep hard references...
406 m_aFlatParaList.insert( xRet );
409 return xRet;
412 uno::Reference< text::XFlatParagraph > SwXFlatParagraphIterator::getLastPara()
413 throw( uno::RuntimeException )
415 return getNextPara();
418 uno::Reference< text::XFlatParagraph > SwXFlatParagraphIterator::getParaAfter(const uno::Reference< text::XFlatParagraph > & xPara)
419 throw ( uno::RuntimeException, lang::IllegalArgumentException )
421 vos::OGuard aGuard(Application::GetSolarMutex());
423 uno::Reference< text::XFlatParagraph > xRet;
424 if (!mpDoc)
425 return xRet;
427 text::XFlatParagraph* pFP = xPara.get();
428 SwXFlatParagraph* pFlatParagraph = static_cast<SwXFlatParagraph*>(pFP);
430 if ( !pFlatParagraph )
431 return xRet;
433 const SwTxtNode* pCurrentNode = pFlatParagraph->getTxtNode();
435 if ( !pCurrentNode )
436 return xRet;
438 SwTxtNode* pNextTxtNode = 0;
439 const SwNodes& rNodes = pCurrentNode->GetDoc()->GetNodes();
441 for( ULONG nCurrentNode = pCurrentNode->GetIndex() + 1; nCurrentNode < rNodes.Count(); ++nCurrentNode )
443 SwNode* pNd = rNodes[ nCurrentNode ];
444 pNextTxtNode = dynamic_cast<SwTxtNode*>(pNd);
445 if ( pNextTxtNode )
446 break;
449 if ( pNextTxtNode )
451 // Expand the string:
452 rtl::OUString aExpandText;
453 const ModelToViewHelper::ConversionMap* pConversionMap =
454 pNextTxtNode->BuildConversionMap( aExpandText );
456 xRet = new SwXFlatParagraph( *pNextTxtNode, aExpandText, pConversionMap );
457 // keep hard references...
458 m_aFlatParaList.insert( xRet );
461 return xRet;
464 uno::Reference< text::XFlatParagraph > SwXFlatParagraphIterator::getParaBefore(const uno::Reference< text::XFlatParagraph > & xPara )
465 throw ( uno::RuntimeException, lang::IllegalArgumentException )
467 vos::OGuard aGuard(Application::GetSolarMutex());
469 uno::Reference< text::XFlatParagraph > xRet;
470 if (!mpDoc)
471 return xRet;
473 text::XFlatParagraph* pFP = xPara.get();
474 SwXFlatParagraph* pFlatParagraph = static_cast<SwXFlatParagraph*>(pFP);
476 if ( !pFlatParagraph )
477 return xRet;
479 const SwTxtNode* pCurrentNode = pFlatParagraph->getTxtNode();
481 if ( !pCurrentNode )
482 return xRet;
484 SwTxtNode* pPrevTxtNode = 0;
485 const SwNodes& rNodes = pCurrentNode->GetDoc()->GetNodes();
487 for( ULONG nCurrentNode = pCurrentNode->GetIndex() - 1; nCurrentNode > 0; --nCurrentNode )
489 SwNode* pNd = rNodes[ nCurrentNode ];
490 pPrevTxtNode = dynamic_cast<SwTxtNode*>(pNd);
491 if ( pPrevTxtNode )
492 break;
495 if ( pPrevTxtNode )
497 // Expand the string:
498 rtl::OUString aExpandText;
499 const ModelToViewHelper::ConversionMap* pConversionMap =
500 pPrevTxtNode->BuildConversionMap( aExpandText );
502 xRet = new SwXFlatParagraph( *pPrevTxtNode, aExpandText, pConversionMap );
503 // keep hard references...
504 m_aFlatParaList.insert( xRet );
507 return xRet;