Line data Source code
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 <hintids.hxx>
21 : #include <editeng/boxitem.hxx>
22 : #include <tblrwcl.hxx>
23 : #include <swtblfmt.hxx>
24 : #include <algorithm>
25 :
26 : using namespace ::editeng;
27 :
28 0 : inline const SvxBorderLine* GetLineTB( const SvxBoxItem* pBox, bool bTop )
29 : {
30 0 : return bTop ? pBox->GetTop() : pBox->GetBottom();
31 : }
32 :
33 0 : bool _SwGCBorder_BoxBrd::CheckLeftBorderOfFormat( const SwFrameFormat& rFormat )
34 : {
35 : const SvxBorderLine* pBrd;
36 : const SfxPoolItem* pItem;
37 0 : if( SfxItemState::SET == rFormat.GetItemState( RES_BOX, true, &pItem ) &&
38 0 : 0 != ( pBrd = static_cast<const SvxBoxItem*>(pItem)->GetLeft() ) )
39 : {
40 0 : if( *pBrdLn == *pBrd )
41 0 : bAnyBorderFnd = true;
42 0 : return true;
43 : }
44 0 : return false;
45 : }
46 :
47 : static bool lcl_GCBorder_ChkBoxBrd_B( const SwTableBox* pBox, _SwGCBorder_BoxBrd* pPara );
48 :
49 0 : static bool lcl_GCBorder_ChkBoxBrd_L( const SwTableLine* pLine, _SwGCBorder_BoxBrd* pPara )
50 : {
51 0 : const SwTableBox* pBox = pLine->GetTabBoxes().front();
52 0 : return lcl_GCBorder_ChkBoxBrd_B( pBox, pPara );
53 : }
54 :
55 0 : static bool lcl_GCBorder_ChkBoxBrd_B( const SwTableBox* pBox, _SwGCBorder_BoxBrd* pPara )
56 : {
57 0 : if( !pBox->GetTabLines().empty() )
58 : {
59 0 : for( auto pLine : pBox->GetTabLines() )
60 : {
61 0 : if (!lcl_GCBorder_ChkBoxBrd_L( pLine, pPara ))
62 : {
63 0 : return false;
64 : }
65 : }
66 0 : return true;
67 : }
68 :
69 0 : return pPara->CheckLeftBorderOfFormat( *pBox->GetFrameFormat() );
70 : }
71 :
72 : static void lcl_GCBorder_GetLastBox_B( const SwTableBox* pBox, SwTableBoxes* pPara );
73 :
74 0 : static void lcl_GCBorder_GetLastBox_L( const SwTableLine* pLine, SwTableBoxes* pPara )
75 : {
76 0 : const SwTableBoxes& rBoxes = pLine->GetTabBoxes();
77 0 : SwTableBox* pBox = rBoxes.back();
78 0 : lcl_GCBorder_GetLastBox_B( pBox, pPara );
79 0 : }
80 :
81 0 : static void lcl_GCBorder_GetLastBox_B( const SwTableBox* pBox, SwTableBoxes* pPara )
82 : {
83 0 : const SwTableLines& rLines = pBox->GetTabLines();
84 0 : if( !rLines.empty() )
85 : {
86 0 : for( const SwTableLine* pLine : rLines )
87 0 : lcl_GCBorder_GetLastBox_L( pLine, pPara );
88 : }
89 : else
90 0 : pPara->push_back( const_cast<SwTableBox*>(pBox) );
91 0 : }
92 :
93 : // Find the "end" of the passed BorderLine. Returns the "Layout"Pos!
94 0 : static sal_uInt16 lcl_FindEndPosOfBorder( const SwCollectTableLineBoxes& rCollTLB,
95 : const SvxBorderLine& rBrdLn, size_t& rStt, bool bTop )
96 : {
97 0 : sal_uInt16 nPos, nLastPos = 0;
98 0 : for( size_t nEnd = rCollTLB.Count(); rStt < nEnd; ++rStt )
99 : {
100 : const SfxPoolItem* pItem;
101 : const SvxBorderLine* pBrd;
102 0 : const SwTableBox& rBox = rCollTLB.GetBox( rStt, &nPos );
103 :
104 0 : if( SfxItemState::SET != rBox.GetFrameFormat()->GetItemState(RES_BOX,true, &pItem )
105 0 : || 0 == ( pBrd = GetLineTB( static_cast<const SvxBoxItem*>(pItem), bTop ))
106 0 : || !( *pBrd == rBrdLn ))
107 0 : break;
108 0 : nLastPos = nPos;
109 : }
110 0 : return nLastPos;
111 : }
112 :
113 0 : static inline const SvxBorderLine* lcl_GCBorder_GetBorder( const SwTableBox& rBox,
114 : bool bTop,
115 : const SfxPoolItem** ppItem )
116 : {
117 0 : return SfxItemState::SET == rBox.GetFrameFormat()->GetItemState( RES_BOX, true, ppItem )
118 0 : ? GetLineTB( static_cast<const SvxBoxItem*>(*ppItem), bTop )
119 0 : : 0;
120 : }
121 :
122 0 : static void lcl_GCBorder_DelBorder( const SwCollectTableLineBoxes& rCollTLB,
123 : size_t& rStt, bool bTop,
124 : const SvxBorderLine& rLine,
125 : const SfxPoolItem* pItem,
126 : sal_uInt16 nEndPos,
127 : SwShareBoxFormats* pShareFormats )
128 : {
129 0 : SwTableBox* pBox = const_cast<SwTableBox*>(&rCollTLB.GetBox( rStt ));
130 : sal_uInt16 nNextPos;
131 0 : const SvxBorderLine* pLn = &rLine;
132 :
133 : do {
134 0 : if( pLn && *pLn == rLine )
135 : {
136 0 : SvxBoxItem aBox( *static_cast<const SvxBoxItem*>(pItem) );
137 0 : if( bTop )
138 0 : aBox.SetLine( 0, SvxBoxItemLine::TOP );
139 : else
140 0 : aBox.SetLine( 0, SvxBoxItemLine::BOTTOM );
141 :
142 0 : if( pShareFormats )
143 0 : pShareFormats->SetAttr( *pBox, aBox );
144 : else
145 0 : pBox->ClaimFrameFormat()->SetFormatAttr( aBox );
146 : }
147 :
148 0 : if( ++rStt >= rCollTLB.Count() )
149 0 : break;
150 :
151 0 : pBox = const_cast<SwTableBox*>(&rCollTLB.GetBox( rStt, &nNextPos ));
152 0 : if( nNextPos > nEndPos )
153 0 : break;
154 :
155 0 : pLn = lcl_GCBorder_GetBorder( *pBox, bTop, &pItem );
156 :
157 0 : } while( true );
158 0 : }
159 :
160 : static void lcl_GC_Box_Border( const SwTableBox* pBox, _SwGCLineBorder* pPara );
161 :
162 0 : void sw_GC_Line_Border( const SwTableLine* pLine, _SwGCLineBorder* pGCPara )
163 : {
164 : // First the right edge with the left edge of the succeeding Box within this Line
165 : {
166 0 : _SwGCBorder_BoxBrd aBPara;
167 : const SvxBorderLine* pBrd;
168 : const SfxPoolItem* pItem;
169 0 : const SwTableBoxes& rBoxes = pLine->GetTabBoxes();
170 0 : for( SwTableBoxes::size_type n = 0, nBoxes = rBoxes.size() - 1; n < nBoxes; ++n )
171 : {
172 0 : SwTableBoxes aBoxes;
173 : {
174 0 : SwTableBox* pBox = rBoxes[ n ];
175 0 : if( pBox->GetSttNd() )
176 0 : aBoxes.insert( aBoxes.begin(), pBox );
177 : else
178 0 : lcl_GCBorder_GetLastBox_B( pBox, &aBoxes );
179 : }
180 :
181 0 : for( SwTableBoxes::size_type i = aBoxes.size(); i; )
182 : {
183 : SwTableBox* pBox;
184 0 : if( SfxItemState::SET == (pBox = aBoxes[ --i ])->GetFrameFormat()->
185 0 : GetItemState( RES_BOX, true, &pItem ) &&
186 0 : 0 != ( pBrd = static_cast<const SvxBoxItem*>(pItem)->GetRight() ) )
187 : {
188 0 : aBPara.SetBorder( *pBrd );
189 0 : const SwTableBox* pNextBox = rBoxes[n+1];
190 0 : if( lcl_GCBorder_ChkBoxBrd_B( pNextBox, &aBPara ) &&
191 0 : aBPara.IsAnyBorderFound() )
192 : {
193 0 : SvxBoxItem aBox( *static_cast<const SvxBoxItem*>(pItem) );
194 0 : aBox.SetLine( 0, SvxBoxItemLine::RIGHT );
195 0 : if( pGCPara->pShareFormats )
196 0 : pGCPara->pShareFormats->SetAttr( *pBox, aBox );
197 : else
198 0 : pBox->ClaimFrameFormat()->SetFormatAttr( aBox );
199 : }
200 : }
201 : }
202 :
203 0 : aBoxes.clear();
204 0 : }
205 : }
206 :
207 : // And now the own bottom edge with the succeeding top edge
208 0 : if( !pGCPara->IsLastLine() )
209 : {
210 0 : SwCollectTableLineBoxes aBottom( false );
211 0 : SwCollectTableLineBoxes aTop( true );
212 :
213 0 : sw_Line_CollectBox( pLine, &aBottom );
214 :
215 0 : const SwTableLine* pNextLine = (*pGCPara->pLines)[ pGCPara->nLinePos+1 ];
216 0 : sw_Line_CollectBox( pNextLine, &aTop );
217 :
218 : // remove all "duplicated" Lines that are the same
219 : sal_uInt16 nBtmPos, nTopPos;
220 :
221 0 : size_t nSttBtm {0};
222 0 : size_t nSttTop {0};
223 0 : const size_t nEndBtm {aBottom.Count()};
224 0 : const size_t nEndTop {aTop.Count()};
225 :
226 0 : const SwTableBox *pBtmBox = &aBottom.GetBox( nSttBtm++, &nBtmPos );
227 0 : const SwTableBox *pTopBox = &aTop.GetBox( nSttTop++, &nTopPos );
228 0 : const SfxPoolItem *pBtmItem = 0, *pTopItem = 0;
229 0 : const SvxBorderLine *pBtmLine(0), *pTopLine(0);
230 0 : bool bGetTopItem = true, bGetBtmItem = true;
231 :
232 : do {
233 0 : if( bGetBtmItem )
234 0 : pBtmLine = lcl_GCBorder_GetBorder( *pBtmBox, false, &pBtmItem );
235 0 : if( bGetTopItem )
236 0 : pTopLine = lcl_GCBorder_GetBorder( *pTopBox, true, &pTopItem );
237 :
238 0 : if( pTopLine && pBtmLine && *pTopLine == *pBtmLine )
239 : {
240 : // We can remove one, but which one?
241 0 : const size_t nSavSttBtm {nSttBtm};
242 0 : const size_t nSavSttTop {nSttTop};
243 : sal_uInt16 nBtmEndPos = ::lcl_FindEndPosOfBorder( aBottom,
244 0 : *pTopLine, nSttBtm, false );
245 0 : if( !nBtmEndPos ) nBtmEndPos = nBtmPos;
246 : sal_uInt16 nTopEndPos = ::lcl_FindEndPosOfBorder( aTop,
247 0 : *pTopLine, nSttTop, true );
248 0 : if( !nTopEndPos ) nTopEndPos = nTopPos;
249 :
250 0 : if( nTopEndPos <= nBtmEndPos )
251 : {
252 : // Delete the TopBorders until BottomEndPos
253 0 : nSttTop = nSavSttTop;
254 0 : if( nTopPos <= nBtmEndPos )
255 : lcl_GCBorder_DelBorder( aTop, --nSttTop, true,
256 : *pBtmLine, pTopItem, nBtmEndPos,
257 0 : pGCPara->pShareFormats );
258 : else
259 0 : nSttBtm = nSavSttBtm;
260 : }
261 : else
262 : {
263 : // Else delete the BottomBorders until TopEndPos
264 0 : nSttBtm = nSavSttBtm;
265 0 : if( nBtmPos <= nTopEndPos )
266 : lcl_GCBorder_DelBorder( aBottom, --nSttBtm, false,
267 : *pTopLine, pBtmItem, nTopEndPos,
268 0 : pGCPara->pShareFormats );
269 : else
270 0 : nSttTop = nSavSttTop;
271 : }
272 0 : nTopPos = nBtmPos;
273 : }
274 :
275 0 : if( nTopPos == nBtmPos )
276 : {
277 0 : if( nSttBtm >= nEndBtm || nSttTop >= nEndTop )
278 : break;
279 :
280 0 : pBtmBox = &aBottom.GetBox( nSttBtm++, &nBtmPos );
281 0 : pTopBox = &aTop.GetBox( nSttTop++, &nTopPos );
282 0 : bGetTopItem = bGetBtmItem = true;
283 : }
284 0 : else if( nTopPos < nBtmPos )
285 : {
286 0 : if( nSttTop >= nEndTop )
287 0 : break;
288 0 : pTopBox = &aTop.GetBox( nSttTop++, &nTopPos );
289 0 : bGetTopItem = true;
290 0 : bGetBtmItem = false;
291 : }
292 : else
293 : {
294 0 : if( nSttBtm >= nEndBtm )
295 0 : break;
296 0 : pBtmBox = &aBottom.GetBox( nSttBtm++, &nBtmPos );
297 0 : bGetTopItem = false;
298 0 : bGetBtmItem = true;
299 : }
300 :
301 0 : } while( true );
302 : }
303 :
304 0 : for( SwTableBoxes::const_iterator it = pLine->GetTabBoxes().begin();
305 0 : it != pLine->GetTabBoxes().end(); ++it)
306 0 : lcl_GC_Box_Border(*it, pGCPara );
307 :
308 0 : ++pGCPara->nLinePos;
309 0 : }
310 :
311 0 : static void lcl_GC_Box_Border( const SwTableBox* pBox, _SwGCLineBorder* pPara )
312 : {
313 0 : if( !pBox->GetTabLines().empty() )
314 : {
315 0 : _SwGCLineBorder aPara( *pBox );
316 0 : aPara.pShareFormats = pPara->pShareFormats;
317 0 : for( const SwTableLine* pLine : pBox->GetTabLines() )
318 0 : sw_GC_Line_Border( pLine, &aPara );
319 : }
320 0 : }
321 :
322 : struct _GCLinePara
323 : {
324 : SwTableLines* pLns;
325 : SwShareBoxFormats* pShareFormats;
326 :
327 715 : _GCLinePara( SwTableLines& rLns, _GCLinePara* pPara = 0 )
328 715 : : pLns( &rLns ), pShareFormats( pPara ? pPara->pShareFormats : 0 )
329 715 : {}
330 : };
331 :
332 : static bool lcl_MergeGCLine(SwTableLine* pLine, _GCLinePara* pPara);
333 :
334 9572 : static bool lcl_MergeGCBox(SwTableBox* pTableBox, _GCLinePara* pPara)
335 : {
336 9572 : if( !pTableBox->GetTabLines().empty() )
337 : {
338 : // ATTENTION: The Line count can change!
339 6 : _GCLinePara aPara( pTableBox->GetTabLines(), pPara );
340 29 : for( SwTableLines::size_type n = 0;
341 23 : n < pTableBox->GetTabLines().size() && lcl_MergeGCLine( pTableBox->GetTabLines()[n], &aPara );
342 : ++n )
343 : ;
344 :
345 6 : if( 1 == pTableBox->GetTabLines().size() )
346 : {
347 : // we have a box with a single line, so we just replace it by the line's boxes
348 0 : SwTableLine* pInsLine = pTableBox->GetUpper();
349 0 : SwTableLine* pCpyLine = pTableBox->GetTabLines()[0];
350 0 : SwTableBoxes::iterator it = std::find( pInsLine->GetTabBoxes().begin(), pInsLine->GetTabBoxes().end(), pTableBox );
351 0 : for( auto pTabBox : pCpyLine->GetTabBoxes() )
352 0 : pTabBox->SetUpper( pInsLine );
353 :
354 : // remove the old box from its parent line
355 0 : it = pInsLine->GetTabBoxes().erase( it );
356 : // insert the nested line's boxes in its place
357 0 : pInsLine->GetTabBoxes().insert( it, pCpyLine->GetTabBoxes().begin(), pCpyLine->GetTabBoxes().end());
358 0 : pCpyLine->GetTabBoxes().clear();
359 : // destroy the removed box
360 0 : delete pTableBox;
361 :
362 0 : return false; // set up anew
363 : }
364 : }
365 9572 : return true;
366 : }
367 :
368 3334 : static bool lcl_MergeGCLine(SwTableLine* pLn, _GCLinePara* pGCPara)
369 : {
370 3334 : SwTableBoxes::size_type nBoxes = pLn->GetTabBoxes().size();
371 3334 : if( nBoxes )
372 : {
373 6668 : while( 1 == nBoxes )
374 : {
375 : // We have a Box with Lines
376 437 : SwTableBox* pBox = pLn->GetTabBoxes().front();
377 437 : if( pBox->GetTabLines().empty() )
378 437 : break;
379 :
380 0 : SwTableLine* pLine = pBox->GetTabLines()[0];
381 :
382 : // pLine turns into the current Line (that is rpLine), the rest is moved
383 : // into the LinesArray past the current one.
384 : // The LinesArray is in pPara!
385 0 : SwTableLines::size_type nLines = pBox->GetTabLines().size();
386 :
387 0 : SwTableLines& rLns = *pGCPara->pLns;
388 0 : sal_uInt16 nInsPos = rLns.GetPos( pLn );
389 : OSL_ENSURE( USHRT_MAX != nInsPos, "Could not find Line!" );
390 :
391 0 : SwTableBox* pUpper = pLn->GetUpper();
392 :
393 0 : rLns.erase( rLns.begin() + nInsPos ); // remove the Line from the array
394 0 : rLns.insert( rLns.begin() + nInsPos, pBox->GetTabLines().begin(), pBox->GetTabLines().end() );
395 :
396 : // JP 31.03.99: Bug 60000
397 : // Pass the attributes of the to-be-deleted Lines to the "inserted" one
398 : const SfxPoolItem* pItem;
399 0 : if( SfxItemState::SET == pLn->GetFrameFormat()->GetItemState(
400 0 : RES_BACKGROUND, true, &pItem ))
401 : {
402 0 : SwTableLines& rBoxLns = pBox->GetTabLines();
403 0 : for( auto pBoxLine : rBoxLns )
404 0 : if( SfxItemState::SET != pBoxLine->GetFrameFormat()->
405 0 : GetItemState( RES_BACKGROUND, true ))
406 0 : pGCPara->pShareFormats->SetAttr( *pBoxLine, *pItem );
407 : }
408 :
409 0 : pBox->GetTabLines().erase( pBox->GetTabLines().begin(), pBox->GetTabLines().begin() + nLines ); // Remove Lines from the array
410 :
411 0 : delete pLn;
412 :
413 : // Set the dependency anew
414 0 : while( nLines-- )
415 0 : rLns[ nInsPos++ ]->SetUpper( pUpper );
416 :
417 0 : pLn = pLine; // and set up anew
418 0 : nBoxes = pLn->GetTabBoxes().size();
419 : }
420 :
421 : // ATTENTION: The number of boxes can change!
422 12906 : for( SwTableBoxes::size_type nLen = 0; nLen < pLn->GetTabBoxes().size(); ++nLen )
423 9572 : if( !lcl_MergeGCBox( pLn->GetTabBoxes()[nLen], pGCPara ))
424 0 : --nLen;
425 : }
426 3334 : return true;
427 : }
428 :
429 : // Clean structure a bit
430 709 : void SwTable::GCLines()
431 : {
432 : // ATTENTION: The Line count can change!
433 709 : _GCLinePara aPara( GetTabLines() );
434 709 : SwShareBoxFormats aShareFormats;
435 709 : aPara.pShareFormats = &aShareFormats;
436 4026 : for( SwTableLines::size_type n = 0; n < GetTabLines().size() &&
437 3317 : lcl_MergeGCLine( GetTabLines()[n], &aPara ); ++n )
438 709 : ;
439 886 : }
440 :
441 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|