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