1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
21 #include <osl/diagnose.h>
22 #include <tools/debug.hxx>
23 #include <editeng/eeitem.hxx>
24 #include <com/sun/star/i18n/WordType.hpp>
25 
26 #include <svl/itemset.hxx>
27 #include <editeng/editeng.hxx>
28 #include <editeng/unoedhlp.hxx>
29 #include <editeng/editdata.hxx>
30 #include <editeng/outliner.hxx>
31 #include <editeng/editobj.hxx>
32 
33 #include <editeng/unofored.hxx>
34 #include "unofored_internal.hxx"
35 
36 using namespace ::com::sun::star;
37 
38 
SvxEditEngineForwarder(EditEngine & rEngine)39 SvxEditEngineForwarder::SvxEditEngineForwarder( EditEngine& rEngine ) :
40     rEditEngine( rEngine )
41 {
42 }
43 
~SvxEditEngineForwarder()44 SvxEditEngineForwarder::~SvxEditEngineForwarder()
45 {
46     // the EditEngine may need to be deleted from the outside
47 }
48 
GetParagraphCount() const49 sal_Int32 SvxEditEngineForwarder::GetParagraphCount() const
50 {
51     return rEditEngine.GetParagraphCount();
52 }
53 
GetTextLen(sal_Int32 nParagraph) const54 sal_Int32 SvxEditEngineForwarder::GetTextLen( sal_Int32 nParagraph ) const
55 {
56     return rEditEngine.GetTextLen( nParagraph );
57 }
58 
GetText(const ESelection & rSel) const59 OUString SvxEditEngineForwarder::GetText( const ESelection& rSel ) const
60 {
61     return convertLineEnd(rEditEngine.GetText(rSel), GetSystemLineEnd());
62 }
63 
GetAttribs(const ESelection & rSel,EditEngineAttribs nOnlyHardAttrib) const64 SfxItemSet SvxEditEngineForwarder::GetAttribs( const ESelection& rSel, EditEngineAttribs nOnlyHardAttrib ) const
65 {
66     if( rSel.nStartPara == rSel.nEndPara )
67     {
68         GetAttribsFlags nFlags = GetAttribsFlags::NONE;
69         switch( nOnlyHardAttrib )
70         {
71         case EditEngineAttribs::All:
72             nFlags = GetAttribsFlags::ALL;
73             break;
74         case EditEngineAttribs::OnlyHard:
75             nFlags = GetAttribsFlags::CHARATTRIBS;
76             break;
77         default:
78             OSL_FAIL("unknown flags for SvxOutlinerForwarder::GetAttribs");
79         }
80 
81         return rEditEngine.GetAttribs( rSel.nStartPara, rSel.nStartPos, rSel.nEndPos, nFlags );
82     }
83     else
84     {
85         return rEditEngine.GetAttribs( rSel, nOnlyHardAttrib );
86     }
87 }
88 
GetParaAttribs(sal_Int32 nPara) const89 SfxItemSet SvxEditEngineForwarder::GetParaAttribs( sal_Int32 nPara ) const
90 {
91     SfxItemSet aSet( rEditEngine.GetParaAttribs( nPara ) );
92 
93     sal_uInt16 nWhich = EE_PARA_START;
94     while( nWhich <= EE_PARA_END )
95     {
96         if( aSet.GetItemState( nWhich ) != SfxItemState::SET )
97         {
98             if( rEditEngine.HasParaAttrib( nPara, nWhich ) )
99                 aSet.Put( rEditEngine.GetParaAttrib( nPara, nWhich ) );
100         }
101         nWhich++;
102     }
103 
104     return aSet;
105 }
106 
SetParaAttribs(sal_Int32 nPara,const SfxItemSet & rSet)107 void SvxEditEngineForwarder::SetParaAttribs( sal_Int32 nPara, const SfxItemSet& rSet )
108 {
109     rEditEngine.SetParaAttribs( nPara, rSet );
110 }
111 
RemoveAttribs(const ESelection & rSelection)112 void SvxEditEngineForwarder::RemoveAttribs( const ESelection& rSelection )
113 {
114     rEditEngine.RemoveAttribs( rSelection, false/*bRemoveParaAttribs*/, 0 );
115 }
116 
GetPool() const117 SfxItemPool* SvxEditEngineForwarder::GetPool() const
118 {
119     return rEditEngine.GetEmptyItemSet().GetPool();
120 }
121 
GetPortions(sal_Int32 nPara,std::vector<sal_Int32> & rList) const122 void SvxEditEngineForwarder::GetPortions( sal_Int32 nPara, std::vector<sal_Int32>& rList ) const
123 {
124     rEditEngine.GetPortions( nPara, rList );
125 }
126 
QuickInsertText(const OUString & rText,const ESelection & rSel)127 void SvxEditEngineForwarder::QuickInsertText( const OUString& rText, const ESelection& rSel )
128 {
129     rEditEngine.QuickInsertText( rText, rSel );
130 }
131 
QuickInsertLineBreak(const ESelection & rSel)132 void SvxEditEngineForwarder::QuickInsertLineBreak( const ESelection& rSel )
133 {
134     rEditEngine.QuickInsertLineBreak( rSel );
135 }
136 
QuickInsertField(const SvxFieldItem & rFld,const ESelection & rSel)137 void SvxEditEngineForwarder::QuickInsertField( const SvxFieldItem& rFld, const ESelection& rSel )
138 {
139     rEditEngine.QuickInsertField( rFld, rSel );
140 }
141 
QuickSetAttribs(const SfxItemSet & rSet,const ESelection & rSel)142 void SvxEditEngineForwarder::QuickSetAttribs( const SfxItemSet& rSet, const ESelection& rSel )
143 {
144     rEditEngine.QuickSetAttribs( rSet, rSel );
145 }
146 
IsValid() const147 bool SvxEditEngineForwarder::IsValid() const
148 {
149     // cannot reliably query EditEngine state
150     // while in the middle of an update
151     return rEditEngine.GetUpdateMode();
152 }
153 
CalcFieldValue(const SvxFieldItem & rField,sal_Int32 nPara,sal_Int32 nPos,std::optional<Color> & rpTxtColor,std::optional<Color> & rpFldColor)154 OUString SvxEditEngineForwarder::CalcFieldValue( const SvxFieldItem& rField, sal_Int32 nPara, sal_Int32 nPos, std::optional<Color>& rpTxtColor, std::optional<Color>& rpFldColor )
155 {
156     return rEditEngine.CalcFieldValue( rField, nPara, nPos, rpTxtColor, rpFldColor );
157 }
158 
FieldClicked(const SvxFieldItem & rField)159 void SvxEditEngineForwarder::FieldClicked( const SvxFieldItem& rField )
160 {
161     rEditEngine.FieldClicked( rField );
162 }
163 
GetSvxEditEngineItemState(EditEngine const & rEditEngine,const ESelection & rSel,sal_uInt16 nWhich)164 SfxItemState GetSvxEditEngineItemState( EditEngine const & rEditEngine, const ESelection& rSel, sal_uInt16 nWhich )
165 {
166     std::vector<EECharAttrib> aAttribs;
167 
168     const SfxPoolItem*  pLastItem = nullptr;
169 
170     SfxItemState eState = SfxItemState::DEFAULT;
171 
172     // check all paragraphs inside the selection
173     for( sal_Int32 nPara = rSel.nStartPara; nPara <= rSel.nEndPara; nPara++ )
174     {
175         SfxItemState eParaState = SfxItemState::DEFAULT;
176 
177         // calculate start and endpos for this paragraph
178         sal_Int32 nPos = 0;
179         if( rSel.nStartPara == nPara )
180             nPos = rSel.nStartPos;
181 
182         sal_Int32 nEndPos = rSel.nEndPos;
183         if( rSel.nEndPara != nPara )
184             nEndPos = rEditEngine.GetTextLen( nPara );
185 
186 
187         // get list of char attribs
188         rEditEngine.GetCharAttribs( nPara, aAttribs );
189 
190         bool bEmpty = true;     // we found no item inside the selection of this paragraph
191         bool bGaps  = false;    // we found items but there are gaps between them
192         sal_Int32 nLastEnd = nPos;
193 
194         const SfxPoolItem* pParaItem = nullptr;
195 
196         for (auto const& attrib : aAttribs)
197         {
198             DBG_ASSERT(attrib.pAttr, "GetCharAttribs gives corrupt data");
199 
200             const bool bEmptyPortion = attrib.nStart == attrib.nEnd;
201             if((!bEmptyPortion && attrib.nStart >= nEndPos) ||
202                (bEmptyPortion && attrib.nStart > nEndPos))
203                 break;  // break if we are already behind our selection
204 
205             if((!bEmptyPortion && attrib.nEnd <= nPos) ||
206                (bEmptyPortion && attrib.nEnd < nPos))
207                 continue;   // or if the attribute ends before our selection
208 
209             if(attrib.pAttr->Which() != nWhich)
210                 continue; // skip if is not the searched item
211 
212             // if we already found an item
213             if( pParaItem )
214             {
215                 // ... and its different to this one than the state is don't care
216                 if(*pParaItem != *(attrib.pAttr))
217                     return SfxItemState::DONTCARE;
218             }
219             else
220                 pParaItem = attrib.pAttr;
221 
222             if( bEmpty )
223                 bEmpty = false;
224 
225             if(!bGaps && attrib.nStart > nLastEnd)
226                 bGaps = true;
227 
228             nLastEnd = attrib.nEnd;
229         }
230 
231         if( !bEmpty && !bGaps && nLastEnd < ( nEndPos - 1 ) )
232             bGaps = true;
233 
234         if( bEmpty )
235             eParaState = SfxItemState::DEFAULT;
236         else if( bGaps )
237             eParaState = SfxItemState::DONTCARE;
238         else
239             eParaState = SfxItemState::SET;
240 
241         // if we already found an item check if we found the same
242         if( pLastItem )
243         {
244             if( (pParaItem == nullptr) || (*pLastItem != *pParaItem) )
245                 return SfxItemState::DONTCARE;
246         }
247         else
248         {
249             pLastItem = pParaItem;
250             eState = eParaState;
251         }
252     }
253 
254     return eState;
255 }
256 
GetItemState(const ESelection & rSel,sal_uInt16 nWhich) const257 SfxItemState SvxEditEngineForwarder::GetItemState( const ESelection& rSel, sal_uInt16 nWhich ) const
258 {
259     return GetSvxEditEngineItemState( rEditEngine, rSel, nWhich );
260 }
261 
GetItemState(sal_Int32 nPara,sal_uInt16 nWhich) const262 SfxItemState SvxEditEngineForwarder::GetItemState( sal_Int32 nPara, sal_uInt16 nWhich ) const
263 {
264     const SfxItemSet& rSet = rEditEngine.GetParaAttribs( nPara );
265     return rSet.GetItemState( nWhich );
266 }
267 
GetLanguage(sal_Int32 nPara,sal_Int32 nIndex) const268 LanguageType SvxEditEngineForwarder::GetLanguage( sal_Int32 nPara, sal_Int32 nIndex ) const
269 {
270     return rEditEngine.GetLanguage(nPara, nIndex);
271 }
272 
GetFieldCount(sal_Int32 nPara) const273 sal_Int32 SvxEditEngineForwarder::GetFieldCount( sal_Int32 nPara ) const
274 {
275     return rEditEngine.GetFieldCount(nPara);
276 }
277 
GetFieldInfo(sal_Int32 nPara,sal_uInt16 nField) const278 EFieldInfo SvxEditEngineForwarder::GetFieldInfo( sal_Int32 nPara, sal_uInt16 nField ) const
279 {
280     return rEditEngine.GetFieldInfo( nPara, nField );
281 }
282 
GetBulletInfo(sal_Int32) const283 EBulletInfo SvxEditEngineForwarder::GetBulletInfo( sal_Int32 ) const
284 {
285     return EBulletInfo();
286 }
287 
GetCharBounds(sal_Int32 nPara,sal_Int32 nIndex) const288 tools::Rectangle SvxEditEngineForwarder::GetCharBounds( sal_Int32 nPara, sal_Int32 nIndex ) const
289 {
290     // EditEngine's 'internal' methods like GetCharacterBounds()
291     // don't rotate for vertical text.
292     Size aSize( rEditEngine.CalcTextWidth(), rEditEngine.GetTextHeight() );
293     // swap width and height
294     tools::Long tmp = aSize.Width();
295     aSize.setWidth(aSize.Height());
296     aSize.setHeight(tmp);
297     bool bIsVertical( rEditEngine.IsVertical() );
298 
299     // #108900# Handle virtual position one-past-the end of the string
300     if( nIndex >= rEditEngine.GetTextLen(nPara) )
301     {
302         tools::Rectangle aLast;
303 
304         if( nIndex )
305         {
306             // use last character, if possible
307             aLast = rEditEngine.GetCharacterBounds( EPosition(nPara, nIndex-1) );
308 
309             // move at end of this last character, make one pixel wide
310             aLast.Move( aLast.Right() - aLast.Left(), 0 );
311             aLast.SetSize( Size(1, aLast.GetHeight()) );
312 
313             // take care for CTL
314             aLast = SvxEditSourceHelper::EEToUserSpace( aLast, aSize, bIsVertical );
315         }
316         else
317         {
318             // #109864# Bounds must lie within the paragraph
319             aLast = GetParaBounds( nPara );
320 
321             // #109151# Don't use paragraph height, but line height
322             // instead. aLast is already CTL-correct
323             if( bIsVertical)
324                 aLast.SetSize( Size( rEditEngine.GetLineHeight(nPara), 1 ) );
325             else
326                 aLast.SetSize( Size( 1, rEditEngine.GetLineHeight(nPara) ) );
327         }
328 
329         return aLast;
330     }
331     else
332     {
333         return SvxEditSourceHelper::EEToUserSpace( rEditEngine.GetCharacterBounds( EPosition(nPara, nIndex) ),
334                                                    aSize, bIsVertical );
335     }
336 }
337 
GetParaBounds(sal_Int32 nPara) const338 tools::Rectangle SvxEditEngineForwarder::GetParaBounds( sal_Int32 nPara ) const
339 {
340     const Point aPnt = rEditEngine.GetDocPosTopLeft( nPara );
341     sal_uLong nWidth;
342     sal_uLong nHeight;
343 
344     if( rEditEngine.IsVertical() )
345     {
346         // Hargl. EditEngine's 'external' methods return the rotated
347         // dimensions, 'internal' methods like GetTextHeight( n )
348         // don't rotate.
349         nWidth = rEditEngine.GetTextHeight( nPara );
350         nHeight = rEditEngine.GetTextHeight();
351         sal_uLong nTextWidth = rEditEngine.GetTextHeight();
352 
353         return tools::Rectangle( nTextWidth - aPnt.Y() - nWidth, 0, nTextWidth - aPnt.Y(), nHeight );
354     }
355     else
356     {
357         nWidth = rEditEngine.CalcTextWidth();
358         nHeight = rEditEngine.GetTextHeight( nPara );
359 
360         return tools::Rectangle( 0, aPnt.Y(), nWidth, aPnt.Y() + nHeight );
361     }
362 }
363 
GetMapMode() const364 MapMode SvxEditEngineForwarder::GetMapMode() const
365 {
366     return rEditEngine.GetRefMapMode();
367 }
368 
GetRefDevice() const369 OutputDevice* SvxEditEngineForwarder::GetRefDevice() const
370 {
371     return rEditEngine.GetRefDevice();
372 }
373 
GetIndexAtPoint(const Point & rPos,sal_Int32 & nPara,sal_Int32 & nIndex) const374 bool SvxEditEngineForwarder::GetIndexAtPoint( const Point& rPos, sal_Int32& nPara, sal_Int32& nIndex ) const
375 {
376     Size aSize( rEditEngine.CalcTextWidth(), rEditEngine.GetTextHeight() );
377     // swap width and height
378     tools::Long tmp = aSize.Width();
379     aSize.setWidth(aSize.Height());
380     aSize.setHeight(tmp);
381     Point aEEPos( SvxEditSourceHelper::UserSpaceToEE( rPos,
382                                                       aSize,
383                                                       rEditEngine.IsVertical() ));
384 
385     EPosition aDocPos = rEditEngine.FindDocPosition( aEEPos );
386 
387     nPara = aDocPos.nPara;
388     nIndex = aDocPos.nIndex;
389 
390     return true;
391 }
392 
GetWordIndices(sal_Int32 nPara,sal_Int32 nIndex,sal_Int32 & nStart,sal_Int32 & nEnd) const393 bool SvxEditEngineForwarder::GetWordIndices( sal_Int32 nPara, sal_Int32 nIndex, sal_Int32& nStart, sal_Int32& nEnd ) const
394 {
395     ESelection aRes = rEditEngine.GetWord( ESelection(nPara, nIndex, nPara, nIndex), css::i18n::WordType::DICTIONARY_WORD );
396 
397     if( aRes.nStartPara == nPara &&
398         aRes.nStartPara == aRes.nEndPara )
399     {
400         nStart = aRes.nStartPos;
401         nEnd = aRes.nEndPos;
402 
403         return true;
404     }
405 
406     return false;
407 }
408 
GetAttributeRun(sal_Int32 & nStartIndex,sal_Int32 & nEndIndex,sal_Int32 nPara,sal_Int32 nIndex,bool bInCell) const409 bool SvxEditEngineForwarder::GetAttributeRun( sal_Int32& nStartIndex, sal_Int32& nEndIndex, sal_Int32 nPara, sal_Int32 nIndex, bool bInCell ) const
410 {
411     SvxEditSourceHelper::GetAttributeRun( nStartIndex, nEndIndex, rEditEngine, nPara, nIndex, bInCell );
412     return true;
413 }
414 
GetLineCount(sal_Int32 nPara) const415 sal_Int32 SvxEditEngineForwarder::GetLineCount( sal_Int32 nPara ) const
416 {
417     return rEditEngine.GetLineCount(nPara);
418 }
419 
GetLineLen(sal_Int32 nPara,sal_Int32 nLine) const420 sal_Int32 SvxEditEngineForwarder::GetLineLen( sal_Int32 nPara, sal_Int32 nLine ) const
421 {
422     return rEditEngine.GetLineLen(nPara, nLine);
423 }
424 
GetLineBoundaries(sal_Int32 & rStart,sal_Int32 & rEnd,sal_Int32 nPara,sal_Int32 nLine) const425 void SvxEditEngineForwarder::GetLineBoundaries( /*out*/sal_Int32 &rStart, /*out*/sal_Int32 &rEnd, sal_Int32 nPara, sal_Int32 nLine ) const
426 {
427     rEditEngine.GetLineBoundaries(rStart, rEnd, nPara, nLine);
428 }
429 
GetLineNumberAtIndex(sal_Int32 nPara,sal_Int32 nIndex) const430 sal_Int32 SvxEditEngineForwarder::GetLineNumberAtIndex( sal_Int32 nPara, sal_Int32 nIndex ) const
431 {
432     return rEditEngine.GetLineNumberAtIndex(nPara, nIndex);
433 }
434 
435 
QuickFormatDoc(bool)436 bool SvxEditEngineForwarder::QuickFormatDoc( bool )
437 {
438     rEditEngine.QuickFormatDoc();
439 
440     return true;
441 }
442 
Delete(const ESelection & rSelection)443 bool SvxEditEngineForwarder::Delete( const ESelection& rSelection )
444 {
445     rEditEngine.QuickDelete( rSelection );
446     rEditEngine.QuickFormatDoc();
447 
448     return true;
449 }
450 
InsertText(const OUString & rStr,const ESelection & rSelection)451 bool SvxEditEngineForwarder::InsertText( const OUString& rStr, const ESelection& rSelection )
452 {
453     rEditEngine.QuickInsertText( rStr, rSelection );
454     rEditEngine.QuickFormatDoc();
455 
456     return true;
457 }
458 
GetDepth(sal_Int32) const459 sal_Int16 SvxEditEngineForwarder::GetDepth( sal_Int32 ) const
460 {
461     // EditEngine does not support outline depth
462     return -1;
463 }
464 
SetDepth(sal_Int32,sal_Int16 nNewDepth)465 bool SvxEditEngineForwarder::SetDepth( sal_Int32, sal_Int16 nNewDepth )
466 {
467     // EditEngine does not support outline depth
468     return nNewDepth == -1;
469 }
470 
GetEmptyItemSetPtr()471 const SfxItemSet * SvxEditEngineForwarder::GetEmptyItemSetPtr()
472 {
473     return &rEditEngine.GetEmptyItemSet();
474 }
475 
AppendParagraph()476 void SvxEditEngineForwarder::AppendParagraph()
477 {
478     rEditEngine.InsertParagraph( rEditEngine.GetParagraphCount(), OUString() );
479 }
480 
AppendTextPortion(sal_Int32 nPara,const OUString & rText,const SfxItemSet &)481 sal_Int32 SvxEditEngineForwarder::AppendTextPortion( sal_Int32 nPara, const OUString &rText, const SfxItemSet & /*rSet*/ )
482 {
483     sal_Int32 nLen = 0;
484 
485     sal_Int32 nParaCount = rEditEngine.GetParagraphCount();
486     DBG_ASSERT( nPara < nParaCount, "paragraph index out of bounds" );
487     if (0 <= nPara && nPara < nParaCount)
488     {
489         nLen = rEditEngine.GetTextLen( nPara );
490         rEditEngine.QuickInsertText( rText, ESelection( nPara, nLen, nPara, nLen ) );
491     }
492 
493     return nLen;
494 }
495 
CopyText(const SvxTextForwarder & rSource)496 void SvxEditEngineForwarder::CopyText(const SvxTextForwarder& rSource)
497 {
498     const SvxEditEngineForwarder* pSourceForwarder = dynamic_cast< const SvxEditEngineForwarder* >( &rSource );
499     if( !pSourceForwarder )
500         return;
501     std::unique_ptr<EditTextObject> pNewTextObject = pSourceForwarder->rEditEngine.CreateTextObject();
502     rEditEngine.SetText( *pNewTextObject );
503 }
504 
505 
506 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
507