xref: /trunk/main/sw/source/core/text/redlnitr.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 
32 #include "hintids.hxx"
33 #include <svl/whiter.hxx>
34 #include <tools/shl.hxx>
35 #ifndef _COM_SUN_STAR_I18N_SCRIPTTYPE_HDL_
36 #include <com/sun/star/i18n/ScriptType.hdl>
37 #endif
38 #include <swmodule.hxx>
39 #include <redline.hxx>      // SwRedline
40 #include <txtatr.hxx>       // SwTxt ...
41 #include <docary.hxx>       // SwRedlineTbl
42 #include <itratr.hxx>       // SwAttrIter
43 #include <ndtxt.hxx>        // SwTxtNode
44 #include <doc.hxx>          // SwDoc
45 #include <rootfrm.hxx>
46 #include <breakit.hxx>
47 #include <vcl/keycodes.hxx>
48 #include <vcl/cmdevt.hxx>
49 #include <vcl/settings.hxx>
50 #include <txtfrm.hxx>       // SwTxtFrm
51 #ifndef _APP_HXX //autogen
52 #include <vcl/svapp.hxx>
53 #endif
54 #include <redlnitr.hxx>
55 #include <extinput.hxx>
56 #include <sfx2/printer.hxx>
57 #include <vcl/window.hxx>
58 
59 using namespace ::com::sun::star;
60 
61 /*************************************************************************
62  *                      SwAttrIter::CtorInitAttrIter()
63  *************************************************************************/
64 void SwAttrIter::CtorInitAttrIter( SwTxtNode& rTxtNode, SwScriptInfo& rScrInf, SwTxtFrm* pFrm )
65 {
66     // Beim HTML-Import kann es vorkommen, dass kein Layout existiert.
67     SwRootFrm* pRootFrm = rTxtNode.getIDocumentLayoutAccess()->GetCurrentLayout();
68     pShell = pRootFrm ? pRootFrm->GetCurrShell() : 0;   //swmod 080218
69 
70     pScriptInfo = &rScrInf;
71 
72     // attributes set at the whole paragraph
73     pAttrSet = rTxtNode.GetpSwAttrSet();
74     // attribute array
75     pHints = rTxtNode.GetpSwpHints();
76 
77     // Build a font matching the default paragraph style:
78     SwFontAccess aFontAccess( &rTxtNode.GetAnyFmtColl(), pShell );
79     delete pFnt;
80     pFnt = new SwFont( *aFontAccess.Get()->GetFont() );
81 
82     // set font to vertical if frame layout is vertical
83     sal_Bool bVertLayout = sal_False;
84     sal_Bool bRTL = sal_False;
85     if ( pFrm )
86     {
87         if ( pFrm->IsVertical() )
88         {
89             bVertLayout = sal_True;
90             pFnt->SetVertical( pFnt->GetOrientation(), sal_True );
91         }
92         bRTL = pFrm->IsRightToLeft();
93     }
94 
95     // Initialize the default attribute of the attribute handler
96     // based on the attribute array cached together with the font.
97     // If any further attributes for the paragraph are given in pAttrSet
98     // consider them during construction of the default array, and apply
99     // them to the font
100     aAttrHandler.Init( aFontAccess.Get()->GetDefault(), pAttrSet,
101                        *rTxtNode.getIDocumentSettingAccess(), pShell, *pFnt, bVertLayout );
102 
103     aMagicNo[SW_LATIN] = aMagicNo[SW_CJK] = aMagicNo[SW_CTL] = NULL;
104 
105     // determine script changes if not already done for current paragraph
106     ASSERT( pScriptInfo, "No script info available");
107     if ( pScriptInfo->GetInvalidity() != STRING_LEN )
108          pScriptInfo->InitScriptInfo( rTxtNode, bRTL );
109 
110     if ( pBreakIt->GetBreakIter().is() )
111     {
112         pFnt->SetActual( SwScriptInfo::WhichFont( 0, 0, pScriptInfo ) );
113 
114         xub_StrLen nChg = 0;
115         sal_uInt16 nCnt = 0;
116 
117         do
118         {
119             nChg = pScriptInfo->GetScriptChg( nCnt );
120             sal_uInt16 nScript = pScriptInfo->GetScriptType( nCnt++ );
121             sal_uInt8 nTmp = 4;
122             switch ( nScript ) {
123                 case i18n::ScriptType::ASIAN :
124                     if( !aMagicNo[SW_CJK] ) nTmp = SW_CJK; break;
125                 case i18n::ScriptType::COMPLEX :
126                     if( !aMagicNo[SW_CTL] ) nTmp = SW_CTL; break;
127                 default:
128                     if( !aMagicNo[SW_LATIN ] ) nTmp = SW_LATIN;
129             }
130             if( nTmp < 4 )
131             {
132                 pFnt->ChkMagic( pShell, nTmp );
133                 pFnt->GetMagic( aMagicNo[ nTmp ], aFntIdx[ nTmp ], nTmp );
134             }
135         } while( nChg < rTxtNode.GetTxt().Len() );
136     }
137     else
138     {
139         pFnt->ChkMagic( pShell, SW_LATIN );
140         pFnt->GetMagic( aMagicNo[ SW_LATIN ], aFntIdx[ SW_LATIN ], SW_LATIN );
141     }
142 
143     nStartIndex = nEndIndex = nPos = nChgCnt = 0;
144     nPropFont = 0;
145     SwDoc* pDoc = rTxtNode.GetDoc();
146     const IDocumentRedlineAccess* pIDRA = rTxtNode.getIDocumentRedlineAccess();
147 
148     const SwExtTextInput* pExtInp = pDoc->GetExtTextInput( rTxtNode );
149     const bool bShow = IDocumentRedlineAccess::IsShowChanges( pIDRA->GetRedlineMode() );
150     if( pExtInp || bShow )
151     {
152         MSHORT nRedlPos = pIDRA->GetRedlinePos( rTxtNode, USHRT_MAX );
153         if( pExtInp || MSHRT_MAX != nRedlPos )
154         {
155             const SvUShorts* pArr = 0;
156             xub_StrLen nInputStt = 0;
157             if( pExtInp )
158             {
159                 pArr = &pExtInp->GetAttrs();
160                 nInputStt = pExtInp->Start()->nContent.GetIndex();
161                 Seek( 0 );
162             }
163 
164             pRedln = new SwRedlineItr( rTxtNode, *pFnt, aAttrHandler, nRedlPos,
165                                         bShow, pArr, nInputStt );
166 
167             if( pRedln->IsOn() )
168                 ++nChgCnt;
169         }
170     }
171 }
172 
173 /*************************************************************************
174  * SwRedlineItr - Der Redline-Iterator
175  *
176  * Folgende Informationen/Zustaende gibt es im RedlineIterator:
177  *
178  * nFirst ist der erste Index der RedlineTbl, der mit dem Absatz ueberlappt.
179  *
180  * nAct ist der zur Zeit aktive ( wenn bOn gesetzt ist ) oder der naechste
181  * in Frage kommende Index.
182  * nStart und nEnd geben die Grenzen des Objekts innerhalb des Absatzes an.
183  *
184  * Wenn bOn gesetzt ist, ist der Font entsprechend manipuliert worden.
185  *
186  * Wenn nAct auf MSHRT_MAX gesetzt wurde ( durch Reset() ), so ist zur Zeit
187  * kein Redline aktiv, nStart und nEnd sind invalid.
188  *************************************************************************/
189 
190 SwRedlineItr::SwRedlineItr( const SwTxtNode& rTxtNd, SwFont& rFnt,
191     SwAttrHandler& rAH, MSHORT nRed, sal_Bool bShw, const SvUShorts *pArr,
192     xub_StrLen nExtStart )
193     : rDoc( *rTxtNd.GetDoc() ), rNd( rTxtNd ), rAttrHandler( rAH ), pSet( 0 ),
194       nNdIdx( rTxtNd.GetIndex() ), nFirst( nRed ),
195       nAct( MSHRT_MAX ), bOn( sal_False ), bShow( bShw )
196 {
197     if( pArr )
198         pExt = new SwExtend( *pArr, nExtStart );
199     else
200         pExt = NULL;
201     Seek( rFnt, 0, STRING_LEN );
202 }
203 
204 SwRedlineItr::~SwRedlineItr()
205 {
206     Clear( NULL );
207     delete pSet;
208     delete pExt;
209 }
210 
211 // Der Return-Wert von SwRedlineItr::Seek gibt an, ob der aktuelle Font
212 // veraendert wurde durch Verlassen (-1) oder Betreten eines Bereichs (+1)
213 
214 short SwRedlineItr::_Seek( SwFont& rFnt, xub_StrLen nNew, xub_StrLen nOld )
215 {
216     short nRet = 0;
217     if( ExtOn() )
218         return 0; // Abkuerzung: wenn wir innerhalb eines ExtendTextInputs sind
219             // kann es keine anderen Attributwechsel (auch nicht durch Redlining) geben
220     if( bShow )
221     {
222         if( bOn )
223         {
224             if( nNew >= nEnd )
225             {
226                 --nRet;
227                 _Clear( &rFnt );    // Wir gehen hinter den aktuellen Bereich
228                 ++nAct;             // und pruefen gleich den naechsten
229             }
230             else if( nNew < nStart )
231             {
232                 --nRet;
233                 _Clear( &rFnt );    // Wir gehen vor den aktuellen Bereich
234                 if( nAct > nFirst )
235                     nAct = nFirst;  // Die Pruefung muss von vorne beginnen
236                 else
237                     return nRet + EnterExtend( rFnt, nNew ); // Es gibt keinen vor uns.
238             }
239             else
240                 return nRet + EnterExtend( rFnt, nNew ); // Wir sind im gleichen Bereich geblieben.
241         }
242         if( MSHRT_MAX == nAct || nOld > nNew )
243             nAct = nFirst;
244 
245         nStart = STRING_LEN;
246         nEnd = STRING_LEN;
247 
248         for( ; nAct < rDoc.GetRedlineTbl().Count() ; ++nAct )
249         {
250             rDoc.GetRedlineTbl()[ nAct ]->CalcStartEnd( nNdIdx, nStart, nEnd );
251 
252             if( nNew < nEnd )
253             {
254                 if( nNew >= nStart ) // der einzig moegliche Kandidat
255                 {
256                     bOn = sal_True;
257                     const SwRedline *pRed = rDoc.GetRedlineTbl()[ nAct ];
258 
259                     if (pSet)
260                         pSet->ClearItem();
261                     else
262                     {
263                         SwAttrPool& rPool =
264                             const_cast<SwDoc&>(rDoc).GetAttrPool();
265                         pSet = new SfxItemSet(rPool, RES_CHRATR_BEGIN, RES_CHRATR_END-1);
266                     }
267 
268                     if( 1 < pRed->GetStackCount() )
269                         FillHints( pRed->GetAuthor( 1 ), pRed->GetType( 1 ) );
270                     FillHints( pRed->GetAuthor(), pRed->GetType() );
271 
272                     SfxWhichIter aIter( *pSet );
273                     MSHORT nWhich = aIter.FirstWhich();
274                     while( nWhich )
275                     {
276                         const SfxPoolItem* pItem;
277                         if( ( nWhich < RES_CHRATR_END ) &&
278                             ( SFX_ITEM_SET == pSet->GetItemState( nWhich, sal_True, &pItem ) ) )
279                         {
280                             SwTxtAttr* pAttr = MakeRedlineTxtAttr(
281                                 const_cast<SwDoc&>(rDoc),
282                                 *const_cast<SfxPoolItem*>(pItem) );
283                             pAttr->SetPriorityAttr( sal_True );
284                             aHints.C40_INSERT( SwTxtAttr, pAttr, aHints.Count());
285                             rAttrHandler.PushAndChg( *pAttr, rFnt );
286                             if( RES_CHRATR_COLOR == nWhich )
287                                 rFnt.SetNoCol( sal_True );
288                         }
289                         nWhich = aIter.NextWhich();
290                     }
291 
292                     ++nRet;
293                 }
294                 break;
295             }
296             nStart = STRING_LEN;
297             nEnd = STRING_LEN;
298         }
299     }
300     return nRet + EnterExtend( rFnt, nNew );
301 }
302 
303 void SwRedlineItr::FillHints( MSHORT nAuthor, RedlineType_t eType )
304 {
305     switch ( eType )
306     {
307         case nsRedlineType_t::REDLINE_INSERT:
308             SW_MOD()->GetInsertAuthorAttr(nAuthor, *pSet);
309             break;
310         case nsRedlineType_t::REDLINE_DELETE:
311             SW_MOD()->GetDeletedAuthorAttr(nAuthor, *pSet);
312             break;
313         case nsRedlineType_t::REDLINE_FORMAT:
314         case nsRedlineType_t::REDLINE_FMTCOLL:
315             SW_MOD()->GetFormatAuthorAttr(nAuthor, *pSet);
316             break;
317         default:
318             break;
319     }
320 }
321 
322 void SwRedlineItr::ChangeTxtAttr( SwFont* pFnt, SwTxtAttr &rHt, sal_Bool bChg )
323 {
324     ASSERT( IsOn(), "SwRedlineItr::ChangeTxtAttr: Off?" );
325 
326     if( !bShow && !pExt )
327         return;
328 
329     if( bChg )
330     {
331         if ( pExt && pExt->IsOn() )
332             rAttrHandler.PushAndChg( rHt, *pExt->GetFont() );
333         else
334             rAttrHandler.PushAndChg( rHt, *pFnt );
335     }
336     else
337     {
338         ASSERT( ! pExt || ! pExt->IsOn(), "Pop of attribute during opened extension" )
339         rAttrHandler.PopAndChg( rHt, *pFnt );
340     }
341 }
342 
343 void SwRedlineItr::_Clear( SwFont* pFnt )
344 {
345     ASSERT( bOn, "SwRedlineItr::Clear: Off?" );
346     bOn = sal_False;
347     while( aHints.Count() )
348     {
349         SwTxtAttr *pPos = aHints[ 0 ];
350         aHints.Remove(0);
351         if( pFnt )
352             rAttrHandler.PopAndChg( *pPos, *pFnt );
353         else
354             rAttrHandler.Pop( *pPos );
355         SwTxtAttr::Destroy(pPos, const_cast<SwDoc&>(rDoc).GetAttrPool() );
356     }
357     if( pFnt )
358         pFnt->SetNoCol( sal_False );
359 }
360 
361 xub_StrLen SwRedlineItr::_GetNextRedln( xub_StrLen nNext )
362 {
363     nNext = NextExtend( nNext );
364     if( !bShow || MSHRT_MAX == nFirst )
365         return nNext;
366     if( MSHRT_MAX == nAct )
367     {
368         nAct = nFirst;
369         rDoc.GetRedlineTbl()[ nAct ]->CalcStartEnd( nNdIdx, nStart, nEnd );
370     }
371     if( bOn || !nStart )
372     {
373         if( nEnd < nNext )
374             nNext = nEnd;
375     }
376     else if( nStart < nNext )
377         nNext = nStart;
378     return nNext;
379 }
380 
381 sal_Bool SwRedlineItr::_ChkSpecialUnderline() const
382 {
383     // Wenn die Unterstreichung oder das Escapement vom Redling kommt,
384     // wenden wir immer das SpecialUnderlining, d.h. die Unterstreichung
385     // unter der Grundlinie an.
386     for( MSHORT i = 0; i < aHints.Count(); ++i )
387     {
388         MSHORT nWhich = aHints[i]->Which();
389         if( RES_CHRATR_UNDERLINE == nWhich ||
390             RES_CHRATR_ESCAPEMENT == nWhich )
391             return sal_True;
392     }
393     return sal_False;
394 }
395 
396 sal_Bool SwRedlineItr::CheckLine( xub_StrLen nChkStart, xub_StrLen nChkEnd )
397 {
398     if( nFirst == MSHRT_MAX )
399         return sal_False;
400     if( nChkEnd == nChkStart ) // Leerzeilen gucken ein Zeichen weiter.
401         ++nChkEnd;
402     xub_StrLen nOldStart = nStart;
403     xub_StrLen nOldEnd = nEnd;
404     xub_StrLen nOldAct = nAct;
405     sal_Bool bRet = sal_False;
406 
407     for( nAct = nFirst; nAct < rDoc.GetRedlineTbl().Count() ; ++nAct )
408     {
409         rDoc.GetRedlineTbl()[ nAct ]->CalcStartEnd( nNdIdx, nStart, nEnd );
410         if( nChkEnd < nStart )
411             break;
412         if( nChkStart <= nEnd && ( nChkEnd > nStart || STRING_LEN == nEnd ) )
413         {
414             bRet = sal_True;
415             break;
416         }
417     }
418 
419     nStart = nOldStart;
420     nEnd = nOldEnd;
421     nAct = nOldAct;
422     return bRet;
423 }
424 
425 void SwExtend::ActualizeFont( SwFont &rFnt, MSHORT nAttr )
426 {
427     if ( nAttr & EXTTEXTINPUT_ATTR_UNDERLINE )
428         rFnt.SetUnderline( UNDERLINE_SINGLE );
429     else if ( nAttr & EXTTEXTINPUT_ATTR_BOLDUNDERLINE )
430         rFnt.SetUnderline( UNDERLINE_BOLD );
431     else if ( nAttr & EXTTEXTINPUT_ATTR_DOTTEDUNDERLINE )
432         rFnt.SetUnderline( UNDERLINE_DOTTED );
433     else if ( nAttr & EXTTEXTINPUT_ATTR_DASHDOTUNDERLINE )
434         rFnt.SetUnderline( UNDERLINE_DOTTED );
435 
436     if ( nAttr & EXTTEXTINPUT_ATTR_REDTEXT )
437         rFnt.SetColor( Color( COL_RED ) );
438 
439     if ( nAttr & EXTTEXTINPUT_ATTR_HIGHLIGHT )
440     {
441         const StyleSettings& rStyleSettings = GetpApp()->GetSettings().GetStyleSettings();
442         rFnt.SetColor( rStyleSettings.GetHighlightTextColor() );
443         rFnt.SetBackColor( new Color( rStyleSettings.GetHighlightColor() ) );
444     }
445     if ( nAttr & EXTTEXTINPUT_ATTR_GRAYWAVELINE )
446         rFnt.SetGreyWave( sal_True );
447 }
448 
449 short SwExtend::Enter( SwFont& rFnt, xub_StrLen nNew )
450 {
451     ASSERT( !Inside(), "SwExtend: Enter without Leave" );
452     ASSERT( !pFnt, "SwExtend: Enter with Font" );
453     nPos = nNew;
454     if( Inside() )
455     {
456         pFnt = new SwFont( rFnt );
457         ActualizeFont( rFnt, rArr[ nPos - nStart ] );
458         return 1;
459     }
460     return 0;
461 }
462 
463 sal_Bool SwExtend::_Leave( SwFont& rFnt, xub_StrLen nNew )
464 {
465     ASSERT( Inside(), "SwExtend: Leave without Enter" );
466     MSHORT nOldAttr = rArr[ nPos - nStart ];
467     nPos = nNew;
468     if( Inside() )
469     {   // Wir sind innerhalb des ExtendText-Bereichs geblieben
470         MSHORT nAttr = rArr[ nPos - nStart ];
471         if( nOldAttr != nAttr ) // Gibt es einen (inneren) Attributwechsel?
472         {
473             rFnt = *pFnt;
474             ActualizeFont( rFnt, nAttr );
475         }
476     }
477     else
478     {
479         rFnt = *pFnt;
480         delete pFnt;
481         pFnt = NULL;
482         return sal_True;
483     }
484     return sal_False;
485 }
486 
487 xub_StrLen SwExtend::Next( xub_StrLen nNext )
488 {
489     if( nPos < nStart )
490     {
491         if( nNext > nStart )
492             nNext = nStart;
493     }
494     else if( nPos < nEnd )
495     {
496         MSHORT nIdx = nPos - nStart;
497         MSHORT nAttr = rArr[ nIdx ];
498         while( ++nIdx < rArr.Count() && nAttr == rArr[ nIdx ] )
499             ; //nothing
500         nIdx = nIdx + nStart;
501         if( nNext > nIdx )
502             nNext = nIdx;
503     }
504     return nNext;
505 }
506