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 #include <editeng/ulspitem.hxx>
21 #include <fmtclds.hxx>
22 #include <fmtfordr.hxx>
23 #include <frmfmt.hxx>
24 #include <frmatr.hxx>
25 #include <frmtool.hxx>
26 #include <colfrm.hxx>
27 #include <pagefrm.hxx>
28 #include <bodyfrm.hxx>
29 #include <rootfrm.hxx>
30 #include <sectfrm.hxx>
31 #include <calbck.hxx>
32 #include <ftnfrm.hxx>
33 #include <IDocumentState.hxx>
34 #include <IDocumentLayoutAccess.hxx>
35 #include <IDocumentUndoRedo.hxx>
36 
SwColumnFrame(SwFrameFormat * pFormat,SwFrame * pSib)37 SwColumnFrame::SwColumnFrame( SwFrameFormat *pFormat, SwFrame* pSib ):
38     SwFootnoteBossFrame( pFormat, pSib )
39 {
40     mnFrameType = SwFrameType::Column;
41     SwBodyFrame* pColBody = new SwBodyFrame( pFormat->GetDoc()->GetDfltFrameFormat(), pSib );
42     pColBody->InsertBehind( this, nullptr ); // ColumnFrames now with BodyFrame
43     SetMaxFootnoteHeight( LONG_MAX );
44 }
45 
DestroyImpl()46 void SwColumnFrame::DestroyImpl()
47 {
48     SwFrameFormat *pFormat = GetFormat();
49     SwDoc *pDoc;
50     if ( !(pDoc = pFormat->GetDoc())->IsInDtor() && pFormat->HasOnlyOneListener() )
51     {
52         //I'm the only one, delete the format.
53         //Get default format before, so the base class can cope with it.
54         pDoc->GetDfltFrameFormat()->Add( this );
55         // tdf#134009, like #i32968# avoid SwUndoFrameFormatDelete creation,
56         // the format is owned by the SwColumnFrame, see lcl_AddColumns()
57         ::sw::UndoGuard const ug(pDoc->GetIDocumentUndoRedo());
58         pDoc->DelFrameFormat( pFormat );
59     }
60 
61     SwFootnoteBossFrame::DestroyImpl();
62 }
63 
~SwColumnFrame()64 SwColumnFrame::~SwColumnFrame()
65 {
66 }
67 
lcl_RemoveColumns(SwLayoutFrame * pCont,sal_uInt16 nCnt)68 static void lcl_RemoveColumns( SwLayoutFrame *pCont, sal_uInt16 nCnt )
69 {
70     OSL_ENSURE( pCont && pCont->Lower() && pCont->Lower()->IsColumnFrame(),
71             "no columns to remove." );
72 
73     SwColumnFrame *pColumn = static_cast<SwColumnFrame*>(pCont->Lower());
74     sw_RemoveFootnotes( pColumn, true, true );
75     while ( pColumn->GetNext() )
76     {
77         OSL_ENSURE( pColumn->GetNext()->IsColumnFrame(),
78                 "neighbor of ColumnFrame is no ColumnFrame." );
79         pColumn = static_cast<SwColumnFrame*>(pColumn->GetNext());
80     }
81     for ( sal_uInt16 i = 0; i < nCnt; ++i )
82     {
83         SwColumnFrame *pTmp = static_cast<SwColumnFrame*>(pColumn->GetPrev());
84         pColumn->Cut();
85         SwFrame::DestroyFrame(pColumn); //format is going to be destroyed in the DTor if needed.
86         pColumn = pTmp;
87     }
88 }
89 
lcl_FindColumns(SwLayoutFrame * pLay,sal_uInt16 nCount)90 static SwLayoutFrame * lcl_FindColumns( SwLayoutFrame *pLay, sal_uInt16 nCount )
91 {
92     SwFrame *pCol = pLay->Lower();
93     if ( pLay->IsPageFrame() )
94         pCol = static_cast<SwPageFrame*>(pLay)->FindBodyCont()->Lower();
95 
96     if ( pCol && pCol->IsColumnFrame() )
97     {
98         SwFrame *pTmp = pCol;
99         sal_uInt16 i;
100         for ( i = 0; pTmp; pTmp = pTmp->GetNext(), ++i )
101             /* do nothing */;
102         return i == nCount ? static_cast<SwLayoutFrame*>(pCol) : nullptr;
103     }
104     return nullptr;
105 }
106 
lcl_AddColumns(SwLayoutFrame * pCont,sal_uInt16 nCount)107 static bool lcl_AddColumns( SwLayoutFrame *pCont, sal_uInt16 nCount )
108 {
109     SwDoc *pDoc = pCont->GetFormat()->GetDoc();
110     const bool bMod = pDoc->getIDocumentState().IsModified();
111 
112     //Formats should be shared whenever possible. If a neighbour already has
113     //the same column settings we can add them to the same format.
114     //The neighbour can be searched using the format, however the owner of the
115     //attribute depends on the frame type.
116     SwLayoutFrame *pAttrOwner = pCont;
117     if ( pCont->IsBodyFrame() )
118         pAttrOwner = pCont->FindPageFrame();
119     SwLayoutFrame *pNeighbourCol = nullptr;
120     SwIterator<SwLayoutFrame,SwFormat> aIter( *pAttrOwner->GetFormat() );
121     SwLayoutFrame *pNeighbour = aIter.First();
122 
123     sal_uInt16 nAdd = 0;
124     SwFrame *pCol = pCont->Lower();
125     if ( pCol && pCol->IsColumnFrame() )
126         for ( nAdd = 1; pCol; pCol = pCol->GetNext(), ++nAdd )
127             /* do nothing */;
128     while ( pNeighbour )
129     {
130         if ( nullptr != (pNeighbourCol = lcl_FindColumns( pNeighbour, nCount+nAdd )) &&
131              pNeighbourCol != pCont )
132             break;
133         pNeighbourCol = nullptr;
134         pNeighbour = aIter.Next();
135     }
136 
137     bool bRet;
138     SwTwips nMax = pCont->IsPageBodyFrame() ?
139                    pCont->FindPageFrame()->GetMaxFootnoteHeight() : LONG_MAX;
140     if ( pNeighbourCol )
141     {
142         bRet = false;
143         SwFrame *pTmp = pCont->Lower();
144         while ( pTmp )
145         {
146             pTmp = pTmp->GetNext();
147             pNeighbourCol = static_cast<SwLayoutFrame*>(pNeighbourCol->GetNext());
148         }
149         for ( sal_uInt16 i = 0; i < nCount; ++i )
150         {
151             SwColumnFrame *pTmpCol = new SwColumnFrame( pNeighbourCol->GetFormat(), pCont );
152             pTmpCol->SetMaxFootnoteHeight( nMax );
153             pTmpCol->InsertBefore( pCont, nullptr );
154             pNeighbourCol = static_cast<SwLayoutFrame*>(pNeighbourCol->GetNext());
155         }
156     }
157     else
158     {
159         bRet = true;
160         // tdf#103359, like #i32968# Inserting columns in the section causes MakeFrameFormat to put
161         // nCount objects of type SwUndoFrameFormat on the undo stack. We don't want them.
162         ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
163         for ( sal_uInt16 i = 0; i < nCount; ++i )
164         {
165             SwFrameFormat *pFormat = pDoc->MakeFrameFormat(OUString(), pDoc->GetDfltFrameFormat());
166             SwColumnFrame *pTmp = new SwColumnFrame( pFormat, pCont );
167             pTmp->SetMaxFootnoteHeight( nMax );
168             pTmp->Paste( pCont );
169         }
170     }
171 
172     if ( !bMod )
173         pDoc->getIDocumentState().ResetModified();
174     return bRet;
175 }
176 
177 /** add or remove columns from a layoutframe.
178  *
179  * Normally, a layoutframe with a column attribute of 1 or 0 columns contains
180  * no columnframe. However, a sectionframe with "footnotes at the end" needs
181  * a columnframe.
182  *
183  * @param rOld
184  * @param rNew
185  * @param bChgFootnote if true, the columnframe will be inserted or removed, if necessary.
186  */
ChgColumns(const SwFormatCol & rOld,const SwFormatCol & rNew,const bool bChgFootnote)187 void SwLayoutFrame::ChgColumns( const SwFormatCol &rOld, const SwFormatCol &rNew,
188     const bool bChgFootnote )
189 {
190     if ( rOld.GetNumCols() <= 1 && rNew.GetNumCols() <= 1 && !bChgFootnote )
191         return;
192     // #i97379#
193     // If current lower is a no text frame, then columns are not allowed
194     if ( Lower() && Lower()->IsNoTextFrame() &&
195          rNew.GetNumCols() > 1 )
196     {
197         return;
198     }
199 
200     sal_uInt16 nNewNum, nOldNum = 1;
201     if( Lower() && Lower()->IsColumnFrame() )
202     {
203         SwFrame* pCol = Lower();
204         while( nullptr != (pCol=pCol->GetNext()) )
205             ++nOldNum;
206     }
207     nNewNum = rNew.GetNumCols();
208     if( !nNewNum )
209         ++nNewNum;
210     bool bAtEnd;
211     if( IsSctFrame() )
212         bAtEnd = static_cast<SwSectionFrame*>(this)->IsAnyNoteAtEnd();
213     else
214         bAtEnd = false;
215 
216     //Setting the column width is only needed for new formats.
217     bool bAdjustAttributes = nOldNum != rOld.GetNumCols();
218 
219     //The content is saved and restored if the column count is different.
220     SwFrame *pSave = nullptr;
221     if( nOldNum != nNewNum || bChgFootnote )
222     {
223         SwDoc *pDoc = GetFormat()->GetDoc();
224         OSL_ENSURE( pDoc, "FrameFormat doesn't return a document." );
225         // SaveContent would also suck up the content of the footnote container
226         // and store it within the normal text flow.
227         if( IsPageBodyFrame() )
228             pDoc->getIDocumentLayoutAccess().GetCurrentLayout()->RemoveFootnotes( static_cast<SwPageFrame*>(GetUpper()) );
229         pSave = ::SaveContent( this );
230 
231         //If columns exist, they get deleted if a column count of 0 or 1 is requested.
232         if ( nNewNum == 1 && !bAtEnd )
233         {
234             ::lcl_RemoveColumns( this, nOldNum );
235             if ( IsBodyFrame() )
236                 SetFrameFormat( pDoc->GetDfltFrameFormat() );
237             else
238                 GetFormat()->SetFormatAttr( SwFormatFillOrder() );
239             if ( pSave )
240                 ::RestoreContent( pSave, this, nullptr );
241             return;
242         }
243         if ( nOldNum == 1 )
244         {
245             if ( IsBodyFrame() )
246                 SetFrameFormat( pDoc->GetColumnContFormat() );
247             else
248                 GetFormat()->SetFormatAttr( SwFormatFillOrder( ATT_LEFT_TO_RIGHT ) );
249             if( !Lower() || !Lower()->IsColumnFrame() )
250                 --nOldNum;
251         }
252         if ( nOldNum > nNewNum )
253         {
254             ::lcl_RemoveColumns( this, nOldNum - nNewNum );
255             bAdjustAttributes = true;
256         }
257         else if( nOldNum < nNewNum )
258         {
259             sal_uInt16 nAdd = nNewNum - nOldNum;
260             bAdjustAttributes = lcl_AddColumns( this, nAdd );
261         }
262     }
263 
264     if ( !bAdjustAttributes )
265     {
266         if ( rOld.GetLineWidth()    != rNew.GetLineWidth() ||
267              rOld.GetWishWidth()    != rNew.GetWishWidth() ||
268              rOld.IsOrtho()         != rNew.IsOrtho() )
269             bAdjustAttributes = true;
270         else
271         {
272             const size_t nCount = std::min( rNew.GetColumns().size(), rOld.GetColumns().size() );
273             for ( size_t i = 0; i < nCount; ++i )
274                 if ( !(rOld.GetColumns()[i] == rNew.GetColumns()[i]) )
275                 {
276                     bAdjustAttributes = true;
277                     break;
278                 }
279         }
280     }
281 
282     //The columns can now be easily adjusted.
283     AdjustColumns( &rNew, bAdjustAttributes );
284 
285     //Don't restore the content before. An earlier restore would trigger useless
286     //actions during setup.
287     if ( pSave )
288     {
289         OSL_ENSURE( Lower() && Lower()->IsLayoutFrame() &&
290                 static_cast<SwLayoutFrame*>(Lower())->Lower() &&
291                 static_cast<SwLayoutFrame*>(Lower())->Lower()->IsLayoutFrame(),
292                 "no column body." );   // ColumnFrames contain BodyFrames
293         ::RestoreContent( pSave, static_cast<SwLayoutFrame*>(static_cast<SwLayoutFrame*>(Lower())->Lower()), nullptr );
294     }
295 }
296 
AdjustColumns(const SwFormatCol * pAttr,bool bAdjustAttributes)297 void SwLayoutFrame::AdjustColumns( const SwFormatCol *pAttr, bool bAdjustAttributes )
298 {
299     if( !Lower()->GetNext() )
300     {
301         Lower()->ChgSize( getFramePrintArea().SSize() );
302         return;
303     }
304 
305     const bool bVert = IsVertical();
306 
307     SwRectFn fnRect = bVert ? ( IsVertLR() ? (IsVertLRBT() ? fnRectVertL2RB2T : fnRectVertL2R) : fnRectVert ) : fnRectHori;
308 
309     //If we have a pointer or we have to configure an attribute, we set the
310     //column widths in any case. Otherwise we check if a configuration is needed.
311     if ( !pAttr )
312     {
313         pAttr = &GetFormat()->GetCol();
314         if ( !bAdjustAttributes )
315         {
316             long nAvail = (getFramePrintArea().*fnRect->fnGetWidth)();
317             for ( SwLayoutFrame *pCol = static_cast<SwLayoutFrame*>(Lower());
318                   pCol;
319                   pCol = static_cast<SwLayoutFrame*>(pCol->GetNext()) )
320                 nAvail -= (pCol->getFrameArea().*fnRect->fnGetWidth)();
321             if ( !nAvail )
322                 return;
323         }
324     }
325 
326     //The columns can now be easily adjusted.
327     //The widths get counted so we can give the reminder to the last one.
328     SwTwips nAvail = (getFramePrintArea().*fnRect->fnGetWidth)();
329     const bool bLine = pAttr->GetLineAdj() != COLADJ_NONE;
330     const sal_uInt16 nMin = bLine ? sal_uInt16( 20 + ( pAttr->GetLineWidth() / 2) ) : 0;
331 
332     const bool bR2L = IsRightToLeft();
333     SwFrame *pCol = bR2L ? GetLastLower() : Lower();
334 
335     // #i27399#
336     // bOrtho means we have to adjust the column frames manually. Otherwise
337     // we may use the values returned by CalcColWidth:
338     const bool bOrtho = pAttr->IsOrtho() && pAttr->GetNumCols() > 0;
339     long nGutter = 0;
340 
341     for ( sal_uInt16 i = 0; i < pAttr->GetNumCols() && pCol; ++i ) //i118878, value returned by GetNumCols() can't be trusted
342     {
343         if( !bOrtho )
344         {
345             const SwTwips nWidth = i == (pAttr->GetNumCols() - 1) ?
346                                    nAvail :
347                                    pAttr->CalcColWidth( i, sal_uInt16( (getFramePrintArea().*fnRect->fnGetWidth)() ) );
348 
349             const Size aColSz = bVert ?
350                                 Size( getFramePrintArea().Width(), nWidth ) :
351                                 Size( nWidth, getFramePrintArea().Height() );
352 
353             pCol->ChgSize( aColSz );
354 
355             // With this, the ColumnBodyFrames from page columns gets adjusted and
356             // their bFixHeight flag is set so they won't shrink/grow.
357             // Don't use the flag with frame columns because BodyFrames in frame
358             // columns can grow/shrink.
359             if( IsBodyFrame() )
360                 static_cast<SwLayoutFrame*>(pCol)->Lower()->ChgSize( aColSz );
361 
362             nAvail -= nWidth;
363         }
364 
365         if ( bOrtho || bAdjustAttributes )
366         {
367             const SwColumn *pC = &pAttr->GetColumns()[i];
368             const SwAttrSet* pSet = pCol->GetAttrSet();
369             SvxLRSpaceItem aLR( pSet->GetLRSpace() );
370 
371             //In order to have enough space for the separation lines, we have to
372             //take them into account here. Every time two columns meet we
373             //calculate a clearance of 20 + half the pen width on the left or
374             //right side, respectively.
375             const sal_uInt16 nLeft = pC->GetLeft();
376             const sal_uInt16 nRight = pC->GetRight();
377 
378             aLR.SetLeft ( nLeft );
379             aLR.SetRight( nRight );
380 
381             if ( bLine )
382             {
383                 if ( i == 0 )
384                 {
385                     aLR.SetRight( std::max( nRight, nMin ) );
386                 }
387                 else if ( i == pAttr->GetNumCols() - 1 )
388                 {
389                     aLR.SetLeft ( std::max( nLeft, nMin ) );
390                 }
391                 else
392                 {
393                     aLR.SetLeft ( std::max( nLeft,  nMin ) );
394                     aLR.SetRight( std::max( nRight, nMin ) );
395                 }
396             }
397 
398             if ( bAdjustAttributes )
399             {
400                 SvxULSpaceItem aUL( pSet->GetULSpace() );
401                 aUL.SetUpper(0);
402                 aUL.SetLower(0);
403 
404                 static_cast<SwLayoutFrame*>(pCol)->GetFormat()->SetFormatAttr( aLR );
405                 static_cast<SwLayoutFrame*>(pCol)->GetFormat()->SetFormatAttr( aUL );
406             }
407 
408             nGutter += aLR.GetLeft() + aLR.GetRight();
409         }
410 
411         pCol = bR2L ? pCol->GetPrev() : pCol->GetNext();
412     }
413 
414     if( bOrtho )
415     {
416         long nInnerWidth = ( nAvail - nGutter ) / pAttr->GetNumCols();
417         pCol = Lower();
418         for( sal_uInt16 i = 0; i < pAttr->GetNumCols() && pCol; pCol = pCol->GetNext(), ++i ) //i118878, value returned by GetNumCols() can't be trusted
419         {
420             SwTwips nWidth;
421             if ( i == pAttr->GetNumCols() - 1 )
422                 nWidth = nAvail;
423             else
424             {
425                 SvxLRSpaceItem aLR( pCol->GetAttrSet()->GetLRSpace() );
426                 nWidth = nInnerWidth + aLR.GetLeft() + aLR.GetRight();
427             }
428             if( nWidth < 0 )
429                 nWidth = 0;
430 
431             const Size aColSz = bVert ?
432                                 Size( getFramePrintArea().Width(), nWidth ) :
433                                 Size( nWidth, getFramePrintArea().Height() );
434 
435             pCol->ChgSize( aColSz );
436 
437             if( IsBodyFrame() )
438                 static_cast<SwLayoutFrame*>(pCol)->Lower()->ChgSize( aColSz );
439 
440             nAvail -= nWidth;
441         }
442     }
443 }
444 
445 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
446