xref: /trunk/main/sw/source/filter/ww8/writerhelper.cxx (revision e32ec1a966a7222d04c4ed2904dd376185cdeba0) !
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 #include <iterator>
27 
28 #include <com/sun/star/util/XCloseable.hpp>
29 
30 #include <doc.hxx>
31 #   include "writerhelper.hxx"
32 #   include <msfilter.hxx>
33 #include <com/sun/star/container/XChild.hpp>
34 #include <com/sun/star/embed/EmbedStates.hpp>
35 
36 #include <algorithm>                //std::swap
37 #   include <svl/itemiter.hxx>  //SfxItemIter
38 #   include <svx/svdobj.hxx>        //SdrObject
39 #   include <svx/svdoole2.hxx>      //SdrOle2Obj
40 #   include <svx/fmglob.hxx>        //FmFormInventor
41 #   include <editeng/brkitem.hxx>       //SvxFmtBreakItem
42 #   include <editeng/tstpitem.hxx>      //SvxTabStopItem
43 #   include <ndtxt.hxx>             //SwTxtNode
44 #    include <ndnotxt.hxx>          //SwNoTxtNode
45 #    include <fmtcntnt.hxx>         //SwFmtCntnt
46 #    include <swtable.hxx>          //SwTable
47 #    include <frmfmt.hxx>           //SwFrmFmt
48 #    include <flypos.hxx>           //SwPosFlyFrms
49 #    include <fmtanchr.hxx>         //SwFmtAnchor
50 #    include <ndgrf.hxx>            //SwGrfNode
51 #    include <fmtfsize.hxx>         //SwFmtFrmSize
52 #   include <SwStyleNameMapper.hxx> //SwStyleNameMapper
53 #   include <docary.hxx>            //SwCharFmts
54 #   include <charfmt.hxx>           //SwCharFmt
55 #   include <fchrfmt.hxx>           //SwFmtCharFmt
56 #ifndef _UNOTOOLS_STREAMWRAP_HXX
57 #   include <unotools/streamwrap.hxx>
58 #endif
59 #include <numrule.hxx>
60 #ifndef _SV_SVAPP_HXX
61 #include <vcl/svapp.hxx>//For i120928
62 #endif
63 #ifdef DEBUGDUMP
64 #       include <vcl/svapp.hxx>
65 #   ifndef _TOOLS_URLOBJ_HXX
66 #       include <tools/urlobj.hxx>
67 #   endif
68 #   ifndef _UNOTOOLS_UCBSTREAMHELPER_HXX
69 #       include <unotools/ucbstreamhelper.hxx>
70 #   endif
71 #       include <unotools/localfilehelper.hxx>
72 #endif
73 
74 using namespace com::sun::star;
75 using namespace nsSwGetPoolIdFromName;
76 
77 
78 namespace
79 {
80     /*
81      Stroustroup forgets copy_if, See C++ Programming language Chp 18, pg 530
82     */
83     template <typename In , typename Out , typename Pred>
my_copy_if(In first,In last,Out res,Pred p)84         Out my_copy_if(In first, In last, Out res, Pred p)
85     {
86         while (first != last)
87         {
88             if (p(*first))
89                 *res = *first;
90             ++first;
91         }
92         return res;
93     }
94 
95     // --> OD 2009-02-04 #i98791# - adjust sorting
96     //Utility to sort SwTxtFmtColl's by their assigned outline style list level
97     class outlinecmp
98     {
99     public:
100         typedef const SwTxtFmtColl* first_argument_type;
101         typedef const SwTxtFmtColl* second_argument_type;
102         typedef bool result_type;
operator ()(const SwTxtFmtColl * pA,const SwTxtFmtColl * pB) const103         bool operator()(const SwTxtFmtColl *pA, const SwTxtFmtColl *pB) const
104         {
105             // --> OD 2009-02-04 #i98791#
106 //            return pA->GetAttrOutlineLevel() < pB->GetAttrOutlineLevel();   //<-end,zhaojianwei
107             bool bResult( false );
108             const bool bIsAAssignedToOutlineStyle( pA->IsAssignedToListLevelOfOutlineStyle() );
109             const bool bIsBAssignedToOutlineStyle( pB->IsAssignedToListLevelOfOutlineStyle() );
110             if ( bIsAAssignedToOutlineStyle != bIsBAssignedToOutlineStyle )
111             {
112                 bResult = bIsBAssignedToOutlineStyle;
113             }
114             else if ( !bIsAAssignedToOutlineStyle )
115             {
116                 // pA and pB are equal regarding the sorting criteria.
117                 // Thus return value does not matter.
118                 bResult = false;
119             }
120             else
121             {
122                 bResult = pA->GetAssignedOutlineStyleLevel() < pB->GetAssignedOutlineStyleLevel();
123             }
124 
125             return bResult;
126             // <--
127        }
128     };
129     // <--
130 
IsValidSlotWhich(sal_uInt16 nSlotId,sal_uInt16 nWhichId)131     bool IsValidSlotWhich(sal_uInt16 nSlotId, sal_uInt16 nWhichId)
132     {
133         return (nSlotId != 0 && nWhichId != 0 && nSlotId != nWhichId);
134     }
135 
136     /*
137      Utility to convert a SwPosFlyFrms into a simple vector of sw::Frames
138 
139      The crucial thing is that a sw::Frame always has an anchor which
140      points to some content in the document. This is a requirement of exporting
141      to Word
142     */
SwPosFlyFrmsToFrames(const SwPosFlyFrms & rFlys)143     sw::Frames SwPosFlyFrmsToFrames(const SwPosFlyFrms &rFlys)
144     {
145         sw::Frames aRet;
146 
147         for(SwPosFlyFrms::const_iterator aIter(rFlys.begin()); aIter != rFlys.end(); aIter++)
148         {
149             const SwFrmFmt &rEntry = (*aIter)->GetFmt();
150 
151             if (const SwPosition* pAnchor = rEntry.GetAnchor().GetCntntAnchor())
152             {
153                 aRet.push_back(sw::Frame(rEntry, *pAnchor));
154             }
155             else
156             {
157                 SwPosition aPos((*aIter)->GetNdIndex());
158 
159                 if (SwTxtNode* pTxtNd = aPos.nNode.GetNode().GetTxtNode())
160                 {
161                     aPos.nContent.Assign(pTxtNd, 0);
162                 }
163 
164                 aRet.push_back(sw::Frame(rEntry, aPos));
165             }
166         }
167         return aRet;
168     }
169 
170     //Utility to test if a frame is anchored at a given node index
171     class anchoredto
172     {
173     public:
174         typedef const sw::Frame& argument_type;
175         typedef bool result_type;
176     private:
177     private:
178         sal_uLong mnNode;
179     public:
anchoredto(sal_uLong nNode)180         anchoredto(sal_uLong nNode) : mnNode(nNode) {}
operator ()(const sw::Frame & rFrame) const181         bool operator()(const sw::Frame &rFrame) const
182         {
183             return (mnNode == rFrame.GetPosition().nNode.GetNode().GetIndex());
184         }
185     };
186 }
187 
188 namespace sw
189 {
190     //For i120928,size conversion before exporting graphic of bullet
Frame(const Graphic & rGrf,const SwPosition & rPos)191     Frame::Frame(const Graphic&rGrf, const SwPosition &rPos)
192         :mpFlyFrm(NULL),
193         maPos(rPos),
194         maSize(),
195         maLayoutSize(),
196         meWriterType(eBulletGrf),
197         mpStartFrameContent(0),
198         mbIsInline(true),
199         mbForBullet(true),
200         maGrf(rGrf)
201     {
202         const MapMode aMap100mm( MAP_100TH_MM );
203         Size    aSize( rGrf.GetPrefSize() );
204         if ( MAP_PIXEL == rGrf.GetPrefMapMode().GetMapUnit() )
205         {
206             aSize = Application::GetDefaultDevice()->PixelToLogic(aSize, aMap100mm );
207         }
208         else
209         {
210             aSize = OutputDevice::LogicToLogic( aSize,rGrf.GetPrefMapMode(), aMap100mm );
211         }
212         maSize = aSize;
213         maLayoutSize = maSize;
214     }
215 
Frame(const SwFrmFmt & rFmt,const SwPosition & rPos)216     Frame::Frame(const SwFrmFmt &rFmt, const SwPosition &rPos)
217         : mpFlyFrm(&rFmt),
218           maPos(rPos),
219           maSize(),
220           // --> OD 2007-04-19 #i43447#
221           maLayoutSize(),
222           // <--
223           meWriterType(eTxtBox),
224           mpStartFrameContent(0),
225           // --> OD 2007-04-19 #i43447# - move to initialization list
226           mbIsInline( (rFmt.GetAnchor().GetAnchorId() == FLY_AS_CHAR) )
227           // <--
228           //For i120928,handle graphic of bullet within existing implementation
229           ,mbForBullet(false)
230           ,maGrf()
231     {
232         switch (rFmt.Which())
233         {
234             case RES_FLYFRMFMT:
235                 if (const SwNodeIndex* pIdx = rFmt.GetCntnt().GetCntntIdx())
236                 {
237                     SwNodeIndex aIdx(*pIdx, 1);
238                     const SwNode &rNd = aIdx.GetNode();
239                     using sw::util::GetSwappedInSize;
240                     // --> OD 2007-04-19 #i43447# - determine layout size
241                     {
242                         SwRect aLayRect( rFmt.FindLayoutRect() );
243                         Rectangle aRect( aLayRect.SVRect() );
244                         // The Object is not rendered (e.g. something in unused
245                         // header/footer) - thus, get the values from the format.
246                         if ( aLayRect.IsEmpty() )
247                         {
248                             aRect.SetSize( rFmt.GetFrmSize().GetSize() );
249                         }
250                         maLayoutSize = aRect.GetSize();
251                     }
252                     // <--
253                     switch (rNd.GetNodeType())
254                     {
255                         case ND_GRFNODE:
256                             meWriterType = eGraphic;
257                             maSize = GetSwappedInSize(*rNd.GetNoTxtNode());
258                             break;
259                         case ND_OLENODE:
260                             meWriterType = eOle;
261                             maSize = GetSwappedInSize(*rNd.GetNoTxtNode());
262                             break;
263                         default:
264                             meWriterType = eTxtBox;
265                             // --> OD 2007-04-19 #i43447#
266                             // Size equals layout size for text boxes
267                             maSize = maLayoutSize;
268                             // <--
269                             break;
270                     }
271                     mpStartFrameContent = &rNd;
272                 }
273                 else
274                 {
275                     ASSERT(sal_False, "Impossible");
276                     meWriterType = eTxtBox;
277                 }
278                 break;
279             default:
280                 if (const SdrObject* pObj = rFmt.FindRealSdrObject())
281                 {
282                     if (pObj->GetObjInventor() == FmFormInventor)
283                         meWriterType = eFormControl;
284                     else
285                         meWriterType = eDrawing;
286                     maSize = pObj->GetSnapRect().GetSize();
287                 }
288                 else
289                 {
290                     ASSERT(sal_False, "Impossible");
291                     meWriterType = eDrawing;
292                 }
293                 break;
294         }
295     }
296 
IsInline() const297     bool Frame::IsInline() const
298     {
299         return mbIsInline;
300     }
301 
ForceTreatAsInline()302     void Frame::ForceTreatAsInline()
303     {
304         mbIsInline = true;
305     }
306 
307     namespace hack
308     {
309 
TransformWhichBetweenPools(const SfxItemPool & rDestPool,const SfxItemPool & rSrcPool,sal_uInt16 nWhich)310         sal_uInt16 TransformWhichBetweenPools(const SfxItemPool &rDestPool,
311             const SfxItemPool &rSrcPool, sal_uInt16 nWhich)
312         {
313             sal_uInt16 nSlotId = rSrcPool.GetSlotId(nWhich);
314             if (IsValidSlotWhich(nSlotId, nWhich))
315                 nWhich = rDestPool.GetWhich(nSlotId);
316             else
317                 nWhich = 0;
318             return nWhich;
319         }
320 
GetSetWhichFromSwDocWhich(const SfxItemSet & rSet,const SwDoc & rDoc,sal_uInt16 nWhich)321         sal_uInt16 GetSetWhichFromSwDocWhich(const SfxItemSet &rSet,
322             const SwDoc &rDoc, sal_uInt16 nWhich)
323         {
324             if (RES_WHICHHINT_END < *(rSet.GetRanges()))
325             {
326                 nWhich = TransformWhichBetweenPools(*rSet.GetPool(),
327                     rDoc.GetAttrPool(), nWhich);
328             }
329             return nWhich;
330         }
331 
DrawingOLEAdaptor(SdrOle2Obj & rObj,SfxObjectShell & rPers)332         DrawingOLEAdaptor::DrawingOLEAdaptor(SdrOle2Obj &rObj,
333             SfxObjectShell &rPers)
334             : msOrigPersistName(rObj.GetPersistName()),
335             mxIPRef(rObj.GetObjRef()), mrPers(rPers),
336             mpGraphic( rObj.GetGraphic() )
337         {
338             //rObj.SetPersistName(String());
339             //rObj.SetObjRef(0);
340             rObj.AbandonObject();
341         }
342 
TransferToDoc(::rtl::OUString & rName)343         bool DrawingOLEAdaptor::TransferToDoc( ::rtl::OUString &rName )
344         {
345             ASSERT(mxIPRef.is(), "Transferring invalid object to doc");
346             if (!mxIPRef.is())
347                 return false;
348 
349             uno::Reference < container::XChild > xChild( mxIPRef, uno::UNO_QUERY );
350             if ( xChild.is() )
351                 xChild->setParent( mrPers.GetModel() );
352 
353             bool bSuccess = mrPers.GetEmbeddedObjectContainer().InsertEmbeddedObject( mxIPRef, rName );
354             if (bSuccess)
355             {
356                 if ( mpGraphic )
357                     ::svt::EmbeddedObjectRef::SetGraphicToContainer( *mpGraphic,
358                                                                     mrPers.GetEmbeddedObjectContainer(),
359                                                                     rName,
360                                                                     ::rtl::OUString() );
361 
362                 //mxIPRef->changeState( embed::EmbedStates::LOADED );
363                 mxIPRef = 0;
364             }
365 
366             return bSuccess;
367         }
368 
~DrawingOLEAdaptor()369         DrawingOLEAdaptor::~DrawingOLEAdaptor()
370         {
371             if (mxIPRef.is())
372             {
373                 DBG_ASSERT( !mrPers.GetEmbeddedObjectContainer().HasEmbeddedObject( mxIPRef ), "Object in adaptor is inserted?!" );
374                 try
375                 {
376                     uno::Reference < com::sun::star::util::XCloseable > xClose( mxIPRef, uno::UNO_QUERY );
377                     if ( xClose.is() )
378                         xClose->close(sal_True);
379                 }
380                 catch ( com::sun::star::util::CloseVetoException& )
381                 {
382                 }
383 
384                 mxIPRef = 0;
385             }
386         }
387 
388 #ifdef DEBUGDUMP
CreateDebuggingStream(const String & rSuffix)389         SvStream *CreateDebuggingStream(const String &rSuffix)
390         {
391             SvStream* pDbgOut = 0;
392             static sal_Int32 nCount;
393             String aFileName(String(RTL_CONSTASCII_STRINGPARAM("wwdbg")));
394             aFileName.Append(String::CreateFromInt32(++nCount));
395             aFileName.Append(rSuffix);
396             String aURLStr;
397             if (::utl::LocalFileHelper::ConvertPhysicalNameToURL(
398                 Application::GetAppFileName(), aURLStr))
399             {
400                 INetURLObject aURL(aURLStr);
401                 aURL.removeSegment();
402                 aURL.removeFinalSlash();
403                 aURL.Append(aFileName);
404 
405                 pDbgOut = ::utl::UcbStreamHelper::CreateStream(
406                     aURL.GetMainURL(INetURLObject::NO_DECODE),
407                     STREAM_TRUNC | STREAM_WRITE);
408             }
409             return pDbgOut;
410         }
411 
DumpStream(const SvStream & rSrc,SvStream & rDest,sal_uInt32 nLen)412         void DumpStream(const SvStream &rSrc, SvStream &rDest, sal_uInt32 nLen)
413         {
414             SvStream &rSource = const_cast<SvStream&>(rSrc);
415             sal_uLong nOrigPos = rSource.Tell();
416             if (nLen == STREAM_SEEK_TO_END)
417             {
418                 rSource.Seek(STREAM_SEEK_TO_END);
419                 nLen = rSource.Tell();
420             }
421             if (nLen - nOrigPos)
422             {
423                 rSource.Seek(nOrigPos);
424                 sal_Char* pDat = new sal_Char[nLen];
425                 rSource.Read(pDat, nLen);
426                 rDest.Write(pDat, nLen);
427                 delete[] pDat;
428                 rSource.Seek(nOrigPos);
429             }
430         }
431 #endif
432     }
433 
434     namespace util
435     {
SV_IMPL_OP_PTRARR_SORT(AuthorInfos,AuthorInfo_Ptr)436         SV_IMPL_OP_PTRARR_SORT(AuthorInfos, AuthorInfo_Ptr)
437 
438         SwTwips MakeSafePositioningValue(SwTwips nIn)
439         {
440             if (nIn > SHRT_MAX)
441                 nIn = SHRT_MAX;
442             else if (nIn < SHRT_MIN)
443                 nIn = SHRT_MIN;
444             return nIn;
445         }
446 
SendObjectToHell(SdrObject & rObject) const447         void SetLayer::SendObjectToHell(SdrObject &rObject) const
448         {
449             SetObjectLayer(rObject, eHell);
450         }
451 
SendObjectToHeaven(SdrObject & rObject) const452         void SetLayer::SendObjectToHeaven(SdrObject &rObject) const
453         {
454             SetObjectLayer(rObject, eHeaven);
455         }
456 
SetObjectLayer(SdrObject & rObject,Layer eLayer) const457         void SetLayer::SetObjectLayer(SdrObject &rObject, Layer eLayer) const
458         {
459             if (FmFormInventor == rObject.GetObjInventor())
460                 rObject.SetLayer(mnFormLayer);
461             else
462             {
463                 switch (eLayer)
464                 {
465                     case eHeaven:
466                         rObject.SetLayer(mnHeavenLayer);
467                         break;
468                     case eHell:
469                         rObject.SetLayer(mnHellLayer);
470                         break;
471                 }
472             }
473         }
474 
475         //SetLayer boilerplate begin
Swap(SetLayer & rOther)476         void SetLayer::Swap(SetLayer& rOther) throw()
477         {
478             std::swap(mnHeavenLayer, rOther.mnHeavenLayer);
479             std::swap(mnHellLayer, rOther.mnHellLayer);
480             std::swap(mnFormLayer, rOther.mnFormLayer);
481         }
482 
483         // --> OD 2004-12-13 #i38889# - by default put objects into the invisible
484         // layers.
SetLayer(const SwDoc & rDoc)485         SetLayer::SetLayer(const SwDoc &rDoc)
486             : mnHeavenLayer(rDoc.GetInvisibleHeavenId()),
487               mnHellLayer(rDoc.GetInvisibleHellId()),
488               mnFormLayer(rDoc.GetInvisibleControlsId())
489         {
490         }
491         // <--
492 
SetLayer(const SetLayer & rOther)493         SetLayer::SetLayer(const SetLayer& rOther) throw()
494             : mnHeavenLayer(rOther.mnHeavenLayer),
495             mnHellLayer(rOther.mnHellLayer),
496             mnFormLayer(rOther.mnFormLayer)
497         {
498         }
499 
operator =(const SetLayer & rOther)500         SetLayer& SetLayer::operator=(const SetLayer& rOther) throw()
501         {
502             SetLayer aTemp(rOther);
503             Swap(aTemp);
504             return *this;
505         }
506         //SetLayer boilerplate end
507 
GetPoolItems(const SfxItemSet & rSet,PoolItems & rItems,bool bExportParentItemSet)508         void GetPoolItems(const SfxItemSet &rSet, PoolItems &rItems, bool bExportParentItemSet )
509         {
510             if( bExportParentItemSet )
511             {
512                 sal_uInt16 nTotal = rSet.TotalCount();
513                 for( sal_uInt16 nItem =0; nItem < nTotal; ++nItem )
514                 {
515                     const SfxPoolItem* pItem = 0;
516                     if( SFX_ITEM_SET == rSet.GetItemState( rSet.GetWhichByPos( nItem ), true, &pItem ) )
517                     {
518                         rItems[pItem->Which()] = pItem;
519                     }
520                 }
521             }
522             else if( rSet.Count())
523             {
524                 SfxItemIter aIter(rSet);
525                 if (const SfxPoolItem *pItem = aIter.GetCurItem())
526                 {
527                     do
528                         rItems[pItem->Which()] = pItem;
529                     while (!aIter.IsAtEnd() && 0 != (pItem = aIter.NextItem()));
530                 }
531             }
532         }
533 
SearchPoolItems(const PoolItems & rItems,sal_uInt16 eType)534         const SfxPoolItem *SearchPoolItems(const PoolItems &rItems,
535             sal_uInt16 eType)
536         {
537             sw::cPoolItemIter aIter = rItems.find(eType);
538             if (aIter != rItems.end())
539                 return aIter->second;
540             return 0;
541         }
542 
ClearOverridesFromSet(const SwFmtCharFmt & rFmt,SfxItemSet & rSet)543         void ClearOverridesFromSet(const SwFmtCharFmt &rFmt, SfxItemSet &rSet)
544         {
545             if (const SwCharFmt* pCharFmt = rFmt.GetCharFmt())
546             {
547                 if (pCharFmt->GetAttrSet().Count())
548                 {
549                     SfxItemIter aIter(pCharFmt->GetAttrSet());
550                     const SfxPoolItem *pItem = aIter.GetCurItem();
551                     do
552                         rSet.ClearItem(pItem->Which());
553                     while (!aIter.IsAtEnd() && 0 != (pItem = aIter.NextItem()));
554                 }
555             }
556         }
557 
GetParaStyles(const SwDoc & rDoc)558         ParaStyles GetParaStyles(const SwDoc &rDoc)
559         {
560             ParaStyles aStyles;
561             typedef ParaStyles::size_type mysizet;
562 
563             const SwTxtFmtColls *pColls = rDoc.GetTxtFmtColls();
564             mysizet nCount = pColls ? pColls->Count() : 0;
565             aStyles.reserve(nCount);
566             for (mysizet nI = 0; nI < nCount; ++nI)
567                 aStyles.push_back((*pColls)[ static_cast< sal_uInt16 >(nI) ]);
568             return aStyles;
569         }
570 
GetParaStyle(SwDoc & rDoc,const String & rName)571         SwTxtFmtColl* GetParaStyle(SwDoc &rDoc, const String& rName)
572         {
573             // Search first in the Doc-Styles
574             SwTxtFmtColl* pColl = rDoc.FindTxtFmtCollByName(rName);
575             if (!pColl)
576             {
577                 // Collection not found, try in Pool ?
578                 sal_uInt16 n = SwStyleNameMapper::GetPoolIdFromUIName(rName,
579                     nsSwGetPoolIdFromName::GET_POOLID_TXTCOLL);
580                 if (n != SAL_MAX_UINT16)       // found or standard
581                     pColl = rDoc.GetTxtCollFromPool(n, false);
582             }
583             return pColl;
584         }
585 
GetCharStyle(SwDoc & rDoc,const String & rName)586         SwCharFmt* GetCharStyle(SwDoc &rDoc, const String& rName)
587         {
588             SwCharFmt *pFmt = rDoc.FindCharFmtByName(rName);
589             if (!pFmt)
590             {
591                 // Collection not found, try in Pool ?
592                 sal_uInt16 n = SwStyleNameMapper::GetPoolIdFromUIName(rName,
593                     nsSwGetPoolIdFromName::GET_POOLID_CHRFMT);
594                 if (n != SAL_MAX_UINT16)       // found or standard
595                     pFmt = rDoc.GetCharFmtFromPool(n);
596             }
597             return pFmt;
598         }
599 
600         // --> OD 2009-02-04 #i98791# - adjust sorting algorithm
SortByAssignedOutlineStyleListLevel(ParaStyles & rStyles)601         void SortByAssignedOutlineStyleListLevel(ParaStyles &rStyles)
602         {
603             std::sort(rStyles.begin(), rStyles.end(), outlinecmp());
604         }
605         // <--
606 
607         /*
608            Utility to extract flyfmts from a document, potentially from a
609            selection.
610            */
GetFrames(const SwDoc & rDoc,SwPaM * pPaM)611         Frames GetFrames(const SwDoc &rDoc, SwPaM *pPaM /*, bool bAll*/)
612         {
613             SwPosFlyFrms aFlys(rDoc.GetAllFlyFmts(pPaM, true));
614             sw::Frames aRet(SwPosFlyFrmsToFrames(aFlys));
615             return aRet;
616         }
617 
618 #if 0
619         Frames GetFramesBetweenNodes(const Frames &rFrames,
620             const SwNode &rStart, const SwNode &rEnd)
621         {
622             Frames aRet;
623             sal_uLong nEnd = rEnd.GetIndex();
624             for (sal_uLong nI = rStart.GetIndex(); nI < nEnd; ++nI)
625             {
626                 my_copy_if(rFrames.begin(), rFrames.end(),
627                     std::back_inserter(aRet), anchoredto(nI));
628             }
629             return aRet;
630 
631         }
632 #endif
GetFramesInNode(const Frames & rFrames,const SwNode & rNode)633         Frames GetFramesInNode(const Frames &rFrames, const SwNode &rNode)
634         {
635             Frames aRet;
636             my_copy_if(rFrames.begin(), rFrames.end(),
637                 std::back_inserter(aRet), anchoredto(rNode.GetIndex()));
638             return aRet;
639         }
640 
GetNumFmtFromTxtNode(const SwTxtNode & rTxtNode)641         const SwNumFmt* GetNumFmtFromTxtNode(const SwTxtNode &rTxtNode)
642         {
643             const SwNumRule *pRule = 0;
644             if (
645                 rTxtNode.IsNumbered() && rTxtNode.IsCountedInList() &&
646                 0 != (pRule = rTxtNode.GetNumRule())
647                 )
648             {
649                 return &(pRule->Get( static_cast< sal_uInt16 >(rTxtNode.GetActualListLevel()) ));
650             }
651 
652             ASSERT(rTxtNode.GetDoc(), "No document for node?, suspicious");
653             if (!rTxtNode.GetDoc())
654                 return 0;
655 
656             if (
657                 rTxtNode.IsNumbered() && rTxtNode.IsCountedInList() &&
658                 0 != (pRule = rTxtNode.GetDoc()->GetOutlineNumRule())
659                 )
660             {
661                 return &(pRule->Get( static_cast< sal_uInt16 >(rTxtNode.GetActualListLevel()) ));
662             }
663 
664             return 0;
665         }
666 
GetNumRuleFromTxtNode(const SwTxtNode & rTxtNode)667         const SwNumRule* GetNumRuleFromTxtNode(const SwTxtNode &rTxtNode)
668         {
669             return GetNormalNumRuleFromTxtNode(rTxtNode);
670         }
671 
GetNormalNumRuleFromTxtNode(const SwTxtNode & rTxtNode)672         const SwNumRule* GetNormalNumRuleFromTxtNode(const SwTxtNode &rTxtNode)
673         {
674             const SwNumRule *pRule = 0;
675 
676             if (
677                 rTxtNode.IsNumbered() && rTxtNode.IsCountedInList() &&
678                 0 != (pRule = rTxtNode.GetNumRule())
679                )
680             {
681                 return pRule;
682             }
683             return 0;
684         }
685 
686 
GetNoTxtNodeFromSwFrmFmt(const SwFrmFmt & rFmt)687         SwNoTxtNode *GetNoTxtNodeFromSwFrmFmt(const SwFrmFmt &rFmt)
688         {
689             const SwNodeIndex *pIndex = rFmt.GetCntnt().GetCntntIdx();
690             ASSERT(pIndex, "No NodeIndex in SwFrmFmt ?, suspicious");
691             if (!pIndex)
692                 return 0;
693             SwNodeIndex aIdx(*pIndex, 1);
694             return aIdx.GetNode().GetNoTxtNode();
695         }
696 
HasPageBreak(const SwNode & rNd)697         bool HasPageBreak(const SwNode &rNd)
698         {
699             const SvxFmtBreakItem *pBreak = 0;
700             if (rNd.IsTableNode() && rNd.GetTableNode())
701             {
702                 const SwTable& rTable = rNd.GetTableNode()->GetTable();
703                 const SwFrmFmt* pApply = rTable.GetFrmFmt();
704                 ASSERT(pApply, "impossible");
705                 if (pApply)
706                     pBreak = &(ItemGet<SvxFmtBreakItem>(*pApply, RES_BREAK));
707             }
708             else if (const SwCntntNode *pNd = rNd.GetCntntNode())
709                 pBreak = &(ItemGet<SvxFmtBreakItem>(*pNd, RES_BREAK));
710 
711             if (pBreak && pBreak->GetBreak() == SVX_BREAK_PAGE_BEFORE)
712                 return true;
713             return false;
714         }
715 
PolygonFromPolyPolygon(const PolyPolygon & rPolyPoly)716         Polygon PolygonFromPolyPolygon(const PolyPolygon &rPolyPoly)
717         {
718             if(1 == rPolyPoly.Count())
719             {
720                 return rPolyPoly[0];
721             }
722             else
723             {
724                 // This method will now just concatenate the polygons contained
725                 // in the given PolyPolygon. Anything else which might be thought of
726                 // for reducing to a single polygon will just need nore power and
727                 // cannot create more correct results.
728                 sal_uInt32 nPointCount(0L);
729                 sal_uInt16 a;
730 
731                 for(a = 0; a < rPolyPoly.Count(); a++)
732                 {
733                     nPointCount += (sal_uInt32)rPolyPoly[a].GetSize();
734                 }
735 
736                 if(nPointCount > 0x0000ffff)
737                 {
738                     DBG_ERROR("PolygonFromPolyPolygon: too many points for a single polygon (!)");
739                     nPointCount = 0x0000ffff;
740                 }
741 
742                 Polygon aRetval((sal_uInt16)nPointCount);
743                 sal_uInt32 nAppendIndex(0L);
744 
745                 for(a = 0; a < rPolyPoly.Count(); a++)
746                 {
747                     const Polygon& rCandidate = rPolyPoly[a];
748 
749                     for(sal_uInt16 b(0); nAppendIndex <= nPointCount && b < rCandidate.GetSize(); b++)
750                     {
751                         aRetval[(sal_uInt16)nAppendIndex++] = rCandidate[b];
752                     }
753                 }
754 
755                 return aRetval;
756             }
757         }
758 
IsStarSymbol(const String & rFontName)759         bool IsStarSymbol(const String &rFontName)
760         {
761             String sFamilyNm(GetFontToken(rFontName, 0));
762             return (sFamilyNm.EqualsIgnoreCaseAscii("starsymbol") ||
763                 sFamilyNm.EqualsIgnoreCaseAscii("opensymbol"));
764         }
765 
GetSwappedInSize(const SwNoTxtNode & rNd)766         Size GetSwappedInSize(const SwNoTxtNode& rNd)
767         {
768             Size aGrTwipSz(rNd.GetTwipSize());
769             if ((!aGrTwipSz.Width() || !aGrTwipSz.Height()))
770             {
771                 SwGrfNode *pGrfNode = const_cast<SwGrfNode*>(rNd.GetGrfNode());
772                 if (pGrfNode && (GRAPHIC_NONE != pGrfNode->GetGrf().GetType()))
773                 {
774                     bool bWasSwappedOut = pGrfNode->GetGrfObj().IsSwappedOut();
775                     pGrfNode->SwapIn();
776                     aGrTwipSz = pGrfNode->GetTwipSize();
777                     if (bWasSwappedOut)
778                         pGrfNode->SwapOut();
779                 }
780             }
781 
782             ASSERT(aGrTwipSz.Width() && aGrTwipSz.Height(), "0 x 0 graphic ?");
783             return aGrTwipSz;
784         }
785 
open(const SwPosition & rPos,const SfxPoolItem & rAttr)786         void RedlineStack::open(const SwPosition& rPos, const SfxPoolItem& rAttr)
787         {
788             ASSERT(rAttr.Which() == RES_FLTR_REDLINE, "not a redline");
789             maStack.push_back(new SwFltStackEntry(rPos,rAttr.Clone()));
790         }
791 
792 
793         class SameOpenRedlineType
794         {
795         public:
796             typedef const SwFltStackEntry* argument_type;
797             typedef bool result_type;
798         private:
799         private:
800             RedlineType_t meType;
801         public:
SameOpenRedlineType(RedlineType_t eType)802             SameOpenRedlineType(RedlineType_t eType) : meType(eType) {}
operator ()(const SwFltStackEntry * pEntry) const803             bool operator()(const SwFltStackEntry *pEntry) const
804             {
805                 const SwFltRedline *pTest = static_cast<const SwFltRedline *>
806                     (pEntry->pAttr);
807                 return (pEntry->bLocked && (pTest->eType == meType));
808             }
809         };
810 
close(const SwPosition & rPos,RedlineType_t eType)811         bool RedlineStack::close(const SwPosition& rPos, RedlineType_t eType)
812         {
813             //Search from end for same type
814             myriter aResult = std::find_if(maStack.rbegin(), maStack.rend(),
815                 SameOpenRedlineType(eType));
816             if (aResult != maStack.rend())
817             {
818                 (*aResult)->SetEndPos(rPos);
819                 return true;
820             }
821             return false;
822         }
823 
824 
825 
closeall(const SwPosition & rPos)826         void RedlineStack::closeall(const SwPosition& rPos)
827         {
828             std::for_each(maStack.begin(), maStack.end(), CloseIfOpen(rPos));
829         }
830 
831 
operator ()(SwFltStackEntry * pEntry)832         void SetInDocAndDelete::operator()(SwFltStackEntry *pEntry)
833         {
834             SwPaM aRegion(pEntry->nMkNode);
835             if (
836                 pEntry->MakeRegion(&mrDoc, aRegion, true) &&
837                 (*aRegion.GetPoint() != *aRegion.GetMark())
838             )
839             {
840                 mrDoc.SetRedlineMode((RedlineMode_t)(nsRedlineMode_t::REDLINE_ON | nsRedlineMode_t::REDLINE_SHOW_INSERT |
841                                          nsRedlineMode_t::REDLINE_SHOW_DELETE));
842                 const SwFltRedline *pFltRedline = static_cast<const SwFltRedline*>
843                     (pEntry->pAttr);
844 
845                 if (USHRT_MAX != pFltRedline->nAutorNoPrev)
846                 {
847                     SwRedlineData aData(pFltRedline->eTypePrev,
848                         pFltRedline->nAutorNoPrev, pFltRedline->aStampPrev, aEmptyStr,
849                         0);
850 
851                     mrDoc.AppendRedline(new SwRedline(aData, aRegion), true);
852                 }
853 
854                 SwRedlineData aData(pFltRedline->eType, pFltRedline->nAutorNo,
855                         pFltRedline->aStamp, aEmptyStr, 0);
856 
857                 mrDoc.AppendRedline(new SwRedline(aData, aRegion), true);
858                 mrDoc.SetRedlineMode((RedlineMode_t)(nsRedlineMode_t::REDLINE_NONE | nsRedlineMode_t::REDLINE_SHOW_INSERT |
859                      nsRedlineMode_t::REDLINE_SHOW_DELETE ));
860             }
861             delete pEntry;
862         }
863 
864 
operator ()(const SwFltStackEntry * pOneE,const SwFltStackEntry * pTwoE) const865         bool CompareRedlines::operator()(const SwFltStackEntry *pOneE,
866             const SwFltStackEntry *pTwoE) const
867         {
868             const SwFltRedline *pOne= static_cast<const SwFltRedline*>
869                 (pOneE->pAttr);
870             const SwFltRedline *pTwo= static_cast<const SwFltRedline*>
871                 (pTwoE->pAttr);
872 
873             //Return the earlier time, if two have the same time, prioritize
874             //inserts over deletes
875             if (pOne->aStamp == pTwo->aStamp)
876                 return (pOne->eType == nsRedlineType_t::REDLINE_INSERT && pTwo->eType != nsRedlineType_t::REDLINE_INSERT);
877             else
878                 return (pOne->aStamp < pTwo->aStamp) ? true : false;
879         }
880 
881 
~RedlineStack()882         RedlineStack::~RedlineStack()
883         {
884             std::sort(maStack.begin(), maStack.end(), CompareRedlines());
885             std::for_each(maStack.begin(), maStack.end(), SetInDocAndDelete(mrDoc));
886         }
887 
AddName(const String & rNm)888         sal_uInt16 WrtRedlineAuthor::AddName( const String& rNm )
889         {
890             sal_uInt16 nRet;
891             typedef std::vector<String>::iterator myiter;
892             myiter aIter = std::find(maAuthors.begin(), maAuthors.end(), rNm);
893             if (aIter != maAuthors.end())
894                 nRet = static_cast< sal_uInt16 >(aIter - maAuthors.begin());
895             else
896             {
897                 nRet = static_cast< sal_uInt16 >(maAuthors.size());
898                 maAuthors.push_back(rNm);
899             }
900             return nRet;
901         }
902 /*
903         std::vector<String> WrtRedlineAuthor::GetNames()
904         {
905             return maAuthors;
906         }
907 */
908     }
909 }
910 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
911