merge the formfield patch from ooo-build
[ooovba.git] / svx / source / unoedit / unoforou.cxx
blob96eef1c0b01026fd49e6e45729d3d7cc76758ec0
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: unoforou.cxx,v $
10 * $Revision: 1.36 $
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_svx.hxx"
34 #include <algorithm>
35 #include <svtools/style.hxx>
36 #include <com/sun/star/i18n/WordType.hpp>
38 #include <svtools/itemset.hxx>
39 #include <svx/editeng.hxx>
40 #include <svx/editdata.hxx>
41 #include <svx/outliner.hxx>
42 #include <unoedhlp.hxx>
43 #include <svx/svdobj.hxx>
44 #include <svtools/poolitem.hxx>
45 #include <vcl/wrkwin.hxx>
46 #include <svx/eeitem.hxx>
48 #include <svx/unoforou.hxx>
49 #include <svx/unofored.hxx>
50 #include <svx/outlobj.hxx>
52 using namespace ::com::sun::star;
54 //------------------------------------------------------------------------
56 SvxOutlinerForwarder::SvxOutlinerForwarder( Outliner& rOutl, SdrObject* pSdrObj /* = 0 */ ) :
57 rOutliner( rOutl ),
58 pSdrObject( pSdrObj ),
59 mpAttribsCache( NULL ),
60 mpParaAttribsCache( NULL ),
61 mnParaAttribsCache( 0 )
65 SvxOutlinerForwarder::~SvxOutlinerForwarder()
67 flushCache();
70 USHORT SvxOutlinerForwarder::GetParagraphCount() const
72 return (USHORT)rOutliner.GetParagraphCount();
75 USHORT SvxOutlinerForwarder::GetTextLen( USHORT nParagraph ) const
77 return rOutliner.GetEditEngine().GetTextLen( nParagraph );
80 String SvxOutlinerForwarder::GetText( const ESelection& rSel ) const
82 //! GetText(ESelection) sollte es wohl auch mal am Outliner geben
83 // solange den Hack fuer die EditEngine uebernehmen:
84 EditEngine* pEditEngine = (EditEngine*)&rOutliner.GetEditEngine();
85 return pEditEngine->GetText( rSel, LINEEND_LF );
88 static SfxItemSet ImplOutlinerForwarderGetAttribs( const ESelection& rSel, BOOL bOnlyHardAttrib, EditEngine& rEditEngine )
90 if( rSel.nStartPara == rSel.nEndPara )
92 sal_uInt8 nFlags = 0;
94 switch( bOnlyHardAttrib )
96 case EditEngineAttribs_All:
97 nFlags = GETATTRIBS_ALL;
98 break;
99 case EditEngineAttribs_HardAndPara:
100 nFlags = GETATTRIBS_PARAATTRIBS|GETATTRIBS_CHARATTRIBS;
101 break;
102 case EditEngineAttribs_OnlyHard:
103 nFlags = GETATTRIBS_CHARATTRIBS;
104 break;
105 default:
106 DBG_ERROR("unknown flags for SvxOutlinerForwarder::GetAttribs");
108 return rEditEngine.GetAttribs( rSel.nStartPara, rSel.nStartPos, rSel.nEndPos, nFlags );
110 else
112 return rEditEngine.GetAttribs( rSel, bOnlyHardAttrib );
116 SfxItemSet SvxOutlinerForwarder::GetAttribs( const ESelection& rSel, BOOL bOnlyHardAttrib ) const
118 if( mpAttribsCache && ( 0 == bOnlyHardAttrib ) )
120 // have we the correct set in cache?
121 if( ((SvxOutlinerForwarder*)this)->maAttribCacheSelection.IsEqual(rSel) )
123 // yes! just return the cache
124 return *mpAttribsCache;
126 else
128 // no, we need delete the old cache
129 delete mpAttribsCache;
130 mpAttribsCache = NULL;
134 //! gibt's das nicht am Outliner ???
135 //! und warum ist GetAttribs an der EditEngine nicht const?
136 EditEngine& rEditEngine = (EditEngine&)rOutliner.GetEditEngine();
138 SfxItemSet aSet( ImplOutlinerForwarderGetAttribs( rSel, bOnlyHardAttrib, rEditEngine ) );
140 if( 0 == bOnlyHardAttrib )
142 mpAttribsCache = new SfxItemSet( aSet );
143 maAttribCacheSelection = rSel;
146 SfxStyleSheet* pStyle = rEditEngine.GetStyleSheet( rSel.nStartPara );
147 if( pStyle )
148 aSet.SetParent( &(pStyle->GetItemSet() ) );
150 return aSet;
153 SfxItemSet SvxOutlinerForwarder::GetParaAttribs( USHORT nPara ) const
155 if( mpParaAttribsCache )
157 // have we the correct set in cache?
158 if( nPara == mnParaAttribsCache )
160 // yes! just return the cache
161 return *mpParaAttribsCache;
163 else
165 // no, we need delete the old cache
166 delete mpParaAttribsCache;
167 mpParaAttribsCache = NULL;
171 mpParaAttribsCache = new SfxItemSet( rOutliner.GetParaAttribs( nPara ) );
172 mnParaAttribsCache = nPara;
174 EditEngine& rEditEngine = (EditEngine&)rOutliner.GetEditEngine();
176 SfxStyleSheet* pStyle = rEditEngine.GetStyleSheet( nPara );
177 if( pStyle )
178 mpParaAttribsCache->SetParent( &(pStyle->GetItemSet() ) );
180 return *mpParaAttribsCache;
183 void SvxOutlinerForwarder::SetParaAttribs( USHORT nPara, const SfxItemSet& rSet )
185 flushCache();
187 const SfxItemSet* pOldParent = rSet.GetParent();
188 if( pOldParent )
189 ((SfxItemSet*)&rSet)->SetParent( NULL );
191 rOutliner.SetParaAttribs( nPara, rSet );
193 if( pOldParent )
194 ((SfxItemSet*)&rSet)->SetParent( pOldParent );
197 void SvxOutlinerForwarder::RemoveAttribs( const ESelection& rSelection, sal_Bool bRemoveParaAttribs, sal_uInt16 nWhich )
199 rOutliner.RemoveAttribs( rSelection, bRemoveParaAttribs, nWhich );
202 SfxItemPool* SvxOutlinerForwarder::GetPool() const
204 return rOutliner.GetEmptyItemSet().GetPool();
207 void SvxOutlinerForwarder::GetPortions( USHORT nPara, SvUShorts& rList ) const
209 ((EditEngine&)rOutliner.GetEditEngine()).GetPortions( nPara, rList );
212 void SvxOutlinerForwarder::QuickInsertText( const String& rText, const ESelection& rSel )
214 flushCache();
215 if( rText.Len() == 0 )
217 rOutliner.QuickDelete( rSel );
219 else
221 rOutliner.QuickInsertText( rText, rSel );
225 void SvxOutlinerForwarder::QuickInsertLineBreak( const ESelection& rSel )
227 flushCache();
228 rOutliner.QuickInsertLineBreak( rSel );
231 void SvxOutlinerForwarder::QuickInsertField( const SvxFieldItem& rFld, const ESelection& rSel )
233 flushCache();
234 rOutliner.QuickInsertField( rFld, rSel );
237 void SvxOutlinerForwarder::QuickSetAttribs( const SfxItemSet& rSet, const ESelection& rSel )
239 flushCache();
240 rOutliner.QuickSetAttribs( rSet, rSel );
243 XubString SvxOutlinerForwarder::CalcFieldValue( const SvxFieldItem& rField, USHORT nPara, USHORT nPos, Color*& rpTxtColor, Color*& rpFldColor )
245 return rOutliner.CalcFieldValue( rField, nPara, nPos, rpTxtColor, rpFldColor );
248 BOOL SvxOutlinerForwarder::IsValid() const
250 // cannot reliably query outliner state
251 // while in the middle of an update
252 return rOutliner.GetUpdateMode();
255 extern USHORT GetSvxEditEngineItemState( EditEngine& rEditEngine, const ESelection& rSel, USHORT nWhich );
257 USHORT SvxOutlinerForwarder::GetItemState( const ESelection& rSel, USHORT nWhich ) const
259 return GetSvxEditEngineItemState( (EditEngine&)rOutliner.GetEditEngine(), rSel, nWhich );
262 USHORT SvxOutlinerForwarder::GetItemState( USHORT nPara, USHORT nWhich ) const
264 const SfxItemSet& rSet = rOutliner.GetParaAttribs( nPara );
265 return rSet.GetItemState( nWhich );
269 void SvxOutlinerForwarder::flushCache()
271 if( mpAttribsCache )
273 delete mpAttribsCache;
274 mpAttribsCache = NULL;
277 if( mpParaAttribsCache )
279 delete mpParaAttribsCache;
280 mpParaAttribsCache = NULL;
284 LanguageType SvxOutlinerForwarder::GetLanguage( USHORT nPara, USHORT nIndex ) const
286 return rOutliner.GetLanguage(nPara, nIndex);
289 USHORT SvxOutlinerForwarder::GetFieldCount( USHORT nPara ) const
291 return rOutliner.GetEditEngine().GetFieldCount(nPara);
294 EFieldInfo SvxOutlinerForwarder::GetFieldInfo( USHORT nPara, USHORT nField ) const
296 return rOutliner.GetEditEngine().GetFieldInfo( nPara, nField );
299 EBulletInfo SvxOutlinerForwarder::GetBulletInfo( USHORT nPara ) const
301 return rOutliner.GetBulletInfo( nPara );
304 Rectangle SvxOutlinerForwarder::GetCharBounds( USHORT nPara, USHORT nIndex ) const
306 // #101701#
307 // EditEngine's 'internal' methods like GetCharacterBounds()
308 // don't rotate for vertical text.
309 Size aSize( rOutliner.CalcTextSize() );
310 ::std::swap( aSize.Width(), aSize.Height() );
311 bool bIsVertical( rOutliner.IsVertical() == TRUE );
313 // #108900# Handle virtual position one-past-the end of the string
314 if( nIndex >= GetTextLen(nPara) )
316 Rectangle aLast;
318 if( nIndex )
320 // use last character, if possible
321 aLast = rOutliner.GetEditEngine().GetCharacterBounds( EPosition(nPara, nIndex-1) );
323 // move at end of this last character, make one pixel wide
324 aLast.Move( aLast.Right() - aLast.Left(), 0 );
325 aLast.SetSize( Size(1, aLast.GetHeight()) );
327 // take care for CTL
328 aLast = SvxEditSourceHelper::EEToUserSpace( aLast, aSize, bIsVertical );
330 else
332 // #109864# Bounds must lie within the paragraph
333 aLast = GetParaBounds( nPara );
335 // #109151# Don't use paragraph height, but line height
336 // instead. aLast is already CTL-correct
337 if( bIsVertical)
338 aLast.SetSize( Size( rOutliner.GetLineHeight(nPara,0), 1 ) );
339 else
340 aLast.SetSize( Size( 1, rOutliner.GetLineHeight(nPara,0) ) );
343 return aLast;
345 else
347 return SvxEditSourceHelper::EEToUserSpace( rOutliner.GetEditEngine().GetCharacterBounds( EPosition(nPara, nIndex) ),
348 aSize, bIsVertical );
352 Rectangle SvxOutlinerForwarder::GetParaBounds( USHORT nPara ) const
354 Point aPnt = rOutliner.GetDocPosTopLeft( nPara );
355 Size aSize = rOutliner.CalcTextSize();
357 if( rOutliner.IsVertical() )
359 // #101701#
360 // Hargl. Outliner's 'external' methods return the rotated
361 // dimensions, 'internal' methods like GetTextHeight( n )
362 // don't rotate.
363 ULONG nWidth = rOutliner.GetTextHeight( nPara );
365 return Rectangle( aSize.Width() - aPnt.Y() - nWidth, 0, aSize.Width() - aPnt.Y(), aSize.Height() );
367 else
369 ULONG nHeight = rOutliner.GetTextHeight( nPara );
371 return Rectangle( 0, aPnt.Y(), aSize.Width(), aPnt.Y() + nHeight );
375 MapMode SvxOutlinerForwarder::GetMapMode() const
377 return rOutliner.GetRefMapMode();
380 OutputDevice* SvxOutlinerForwarder::GetRefDevice() const
382 return rOutliner.GetRefDevice();
385 sal_Bool SvxOutlinerForwarder::GetIndexAtPoint( const Point& rPos, USHORT& nPara, USHORT& nIndex ) const
387 // #101701#
388 Size aSize( rOutliner.CalcTextSize() );
389 ::std::swap( aSize.Width(), aSize.Height() );
390 Point aEEPos( SvxEditSourceHelper::UserSpaceToEE( rPos,
391 aSize,
392 rOutliner.IsVertical() == TRUE ));
394 EPosition aDocPos = rOutliner.GetEditEngine().FindDocPosition( aEEPos );
396 nPara = aDocPos.nPara;
397 nIndex = aDocPos.nIndex;
399 return sal_True;
402 sal_Bool SvxOutlinerForwarder::GetWordIndices( USHORT nPara, USHORT nIndex, USHORT& nStart, USHORT& nEnd ) const
404 ESelection aRes = rOutliner.GetEditEngine().GetWord( ESelection(nPara, nIndex, nPara, nIndex), com::sun::star::i18n::WordType::DICTIONARY_WORD );
406 if( aRes.nStartPara == nPara &&
407 aRes.nStartPara == aRes.nEndPara )
409 nStart = aRes.nStartPos;
410 nEnd = aRes.nEndPos;
412 return sal_True;
415 return sal_False;
418 sal_Bool SvxOutlinerForwarder::GetAttributeRun( USHORT& nStartIndex, USHORT& nEndIndex, USHORT nPara, USHORT nIndex ) const
420 return SvxEditSourceHelper::GetAttributeRun( nStartIndex, nEndIndex, rOutliner.GetEditEngine(), nPara, nIndex );
423 USHORT SvxOutlinerForwarder::GetLineCount( USHORT nPara ) const
425 return static_cast < USHORT >( rOutliner.GetLineCount(nPara) );
428 USHORT SvxOutlinerForwarder::GetLineLen( USHORT nPara, USHORT nLine ) const
430 return rOutliner.GetLineLen(nPara, nLine);
433 void SvxOutlinerForwarder::GetLineBoundaries( /*out*/USHORT &rStart, /*out*/USHORT &rEnd, USHORT nPara, USHORT nLine ) const
435 return rOutliner.GetEditEngine().GetLineBoundaries( rStart, rEnd, nPara, nLine );
438 USHORT SvxOutlinerForwarder::GetLineNumberAtIndex( USHORT nPara, USHORT nIndex ) const
440 return rOutliner.GetEditEngine().GetLineNumberAtIndex( nPara, nIndex );
443 sal_Bool SvxOutlinerForwarder::QuickFormatDoc( BOOL )
445 rOutliner.QuickFormatDoc();
447 return sal_True;
450 sal_Bool SvxOutlinerForwarder::Delete( const ESelection& rSelection )
452 flushCache();
453 rOutliner.QuickDelete( rSelection );
454 rOutliner.QuickFormatDoc();
456 return sal_True;
459 sal_Bool SvxOutlinerForwarder::InsertText( const String& rStr, const ESelection& rSelection )
461 flushCache();
462 rOutliner.QuickInsertText( rStr, rSelection );
463 rOutliner.QuickFormatDoc();
465 return sal_True;
468 sal_Int16 SvxOutlinerForwarder::GetDepth( USHORT nPara ) const
470 DBG_ASSERT( nPara < GetParagraphCount(), "SvxOutlinerForwarder::GetDepth: Invalid paragraph index");
472 Paragraph* pPara = rOutliner.GetParagraph( nPara );
474 sal_Int16 nLevel = -1;
476 if( pPara )
477 nLevel = rOutliner.GetDepth( nPara );
479 return nLevel;
482 sal_Bool SvxOutlinerForwarder::SetDepth( USHORT nPara, sal_Int16 nNewDepth )
484 DBG_ASSERT( nPara < GetParagraphCount(), "SvxOutlinerForwarder::SetDepth: Invalid paragraph index");
486 if( (nNewDepth >= -1) && (nNewDepth <= 9) && (nPara < GetParagraphCount()) )
488 Paragraph* pPara = rOutliner.GetParagraph( nPara );
489 if( pPara )
491 rOutliner.SetDepth( pPara, nNewDepth );
493 const bool bOutlinerText = pSdrObject && (pSdrObject->GetObjInventor() == SdrInventor) && (pSdrObject->GetObjIdentifier() == OBJ_OUTLINETEXT);
494 if( bOutlinerText )
495 rOutliner.SetLevelDependendStyleSheet( nPara );
497 return sal_True;
501 return sal_False;
504 sal_Int16 SvxOutlinerForwarder::GetNumberingStartValue( sal_uInt16 nPara )
506 if( nPara < GetParagraphCount() )
508 return rOutliner.GetNumberingStartValue( nPara );
510 else
512 DBG_ERROR( "SvxOutlinerForwarder::GetNumberingStartValue)(), Invalid paragraph index");
513 return -1;
517 void SvxOutlinerForwarder::SetNumberingStartValue( sal_uInt16 nPara, sal_Int16 nNumberingStartValue )
519 if( nPara < GetParagraphCount() )
521 rOutliner.SetNumberingStartValue( nPara, nNumberingStartValue );
523 else
525 DBG_ERROR( "SvxOutlinerForwarder::SetNumberingStartValue)(), Invalid paragraph index");
529 sal_Bool SvxOutlinerForwarder::IsParaIsNumberingRestart( sal_uInt16 nPara )
531 if( nPara < GetParagraphCount() )
533 return rOutliner.IsParaIsNumberingRestart( nPara );
535 else
537 DBG_ERROR( "SvxOutlinerForwarder::IsParaIsNumberingRestart)(), Invalid paragraph index");
538 return sal_False;
542 void SvxOutlinerForwarder::SetParaIsNumberingRestart( sal_uInt16 nPara, sal_Bool bParaIsNumberingRestart )
544 if( nPara < GetParagraphCount() )
546 rOutliner.SetParaIsNumberingRestart( nPara, bParaIsNumberingRestart );
548 else
550 DBG_ERROR( "SvxOutlinerForwarder::SetParaIsNumberingRestart)(), Invalid paragraph index");
554 const SfxItemSet * SvxOutlinerForwarder::GetEmptyItemSetPtr()
556 EditEngine& rEditEngine = const_cast< EditEngine& >( rOutliner.GetEditEngine() );
557 return &rEditEngine.GetEmptyItemSet();
560 void SvxOutlinerForwarder::AppendParagraph()
562 EditEngine& rEditEngine = const_cast< EditEngine& >( rOutliner.GetEditEngine() );
563 rEditEngine.InsertParagraph( rEditEngine.GetParagraphCount(), String::EmptyString() );
566 xub_StrLen SvxOutlinerForwarder::AppendTextPortion( USHORT nPara, const String &rText, const SfxItemSet & /*rSet*/ )
568 xub_StrLen nLen = 0;
570 EditEngine& rEditEngine = const_cast< EditEngine& >( rOutliner.GetEditEngine() );
571 USHORT nParaCount = rEditEngine.GetParagraphCount();
572 DBG_ASSERT( nPara < nParaCount, "paragraph index out of bounds" );
573 if (/*0 <= nPara && */nPara < nParaCount)
575 nLen = rEditEngine.GetTextLen( nPara );
576 rEditEngine.QuickInsertText( rText, ESelection( nPara, nLen, nPara, nLen ) );
579 return nLen;
582 void SvxOutlinerForwarder::CopyText(const SvxTextForwarder& rSource)
584 const SvxOutlinerForwarder* pSourceForwarder = dynamic_cast< const SvxOutlinerForwarder* >( &rSource );
585 if( !pSourceForwarder )
586 return;
587 OutlinerParaObject* pNewOutlinerParaObject = pSourceForwarder->rOutliner.CreateParaObject();
588 rOutliner.SetText( *pNewOutlinerParaObject );
589 delete pNewOutlinerParaObject;
592 //------------------------------------------------------------------------
595 sal_Int16 SvxTextForwarder::GetNumberingStartValue( sal_uInt16 )
597 return -1;
600 void SvxTextForwarder::SetNumberingStartValue( sal_uInt16, sal_Int16 )
604 sal_Bool SvxTextForwarder::IsParaIsNumberingRestart( sal_uInt16 )
606 return sal_False;
609 void SvxTextForwarder::SetParaIsNumberingRestart( sal_uInt16, sal_Bool )
613 //------------------------------------------------------------------------