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 0 : bool _SwGCBorder_BoxBrd::CheckLeftBorderOfFormat( const SwFrmFmt& rFmt )
35 : {
36 : const SvxBorderLine* pBrd;
37 : const SfxPoolItem* pItem;
38 0 : if( SfxItemState::SET == rFmt.GetItemState( RES_BOX, true, &pItem ) &&
39 0 : 0 != ( pBrd = ((SvxBoxItem*)pItem)->GetLeft() ) )
40 : {
41 0 : if( *pBrdLn == *pBrd )
42 0 : bAnyBorderFnd = true;
43 0 : return true;
44 : }
45 0 : return false;
46 : }
47 :
48 : static bool lcl_GCBorder_ChkBoxBrd_B( const SwTableBox* pBox, _SwGCBorder_BoxBrd* pPara );
49 :
50 0 : static bool lcl_GCBorder_ChkBoxBrd_L( const SwTableLine* pLine, _SwGCBorder_BoxBrd* pPara )
51 : {
52 0 : const SwTableBox* pBox = pLine->GetTabBoxes().front();
53 0 : return lcl_GCBorder_ChkBoxBrd_B( pBox, pPara );
54 : }
55 :
56 0 : static bool lcl_GCBorder_ChkBoxBrd_B( const SwTableBox* pBox, _SwGCBorder_BoxBrd* pPara )
57 : {
58 0 : bool bRet = true;
59 0 : if( !pBox->GetTabLines().empty() )
60 : {
61 0 : for( sal_uInt16 n = 0, nLines = pBox->GetTabLines().size();
62 0 : n < nLines && bRet; ++n )
63 : {
64 0 : const SwTableLine* pLine = pBox->GetTabLines()[ n ];
65 0 : bRet = lcl_GCBorder_ChkBoxBrd_L( pLine, pPara );
66 : }
67 : }
68 : else
69 : {
70 0 : bRet = pPara->CheckLeftBorderOfFormat( *pBox->GetFrmFmt() );
71 : }
72 0 : return bRet;
73 : }
74 :
75 : static void lcl_GCBorder_GetLastBox_B( const SwTableBox* pBox, SwTableBoxes* pPara );
76 :
77 0 : static void lcl_GCBorder_GetLastBox_L( const SwTableLine* pLine, SwTableBoxes* pPara )
78 : {
79 0 : const SwTableBoxes& rBoxes = pLine->GetTabBoxes();
80 0 : SwTableBox* pBox = rBoxes.back();
81 0 : lcl_GCBorder_GetLastBox_B( pBox, pPara );
82 0 : }
83 :
84 0 : static void lcl_GCBorder_GetLastBox_B( const SwTableBox* pBox, SwTableBoxes* pPara )
85 : {
86 0 : const SwTableLines& rLines = pBox->GetTabLines();
87 0 : if( !rLines.empty() )
88 : {
89 0 : BOOST_FOREACH( const SwTableLine* pLine, rLines )
90 0 : lcl_GCBorder_GetLastBox_L( pLine, pPara );
91 : }
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( SfxItemState::SET != rBox.GetFrmFmt()->GetItemState(RES_BOX,true, &pItem )
108 0 : || 0 == ( pBrd = GetLineTB( (SvxBoxItem*)pItem, bTop ))
109 0 : || !( *pBrd == rBrdLn ))
110 0 : 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 SfxItemState::SET == rBox.GetFrmFmt()->GetItemState( RES_BOX, 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 : 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 0 : } 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( SfxItemState::SET == (pBox = aBoxes[ --i ])->GetFrmFmt()->
187 0 : GetItemState( RES_BOX, 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( false );
212 0 : SwCollectTblLineBoxes aTop( 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 : 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 0 : if( nTopEndPos <= nBtmEndPos )
248 : {
249 : // Delete the TopBorders until BottomEndPos
250 0 : nSttTop = nSavSttTop;
251 0 : if( nTopPos <= nBtmEndPos )
252 : lcl_GCBorder_DelBorder( aTop, --nSttTop, true,
253 : *pBtmLine, pTopItem, nBtmEndPos,
254 0 : pGCPara->pShareFmts );
255 : else
256 0 : nSttBtm = nSavSttBtm;
257 : }
258 : else
259 : {
260 : // Else delete the BottomBorders until TopEndPos
261 0 : nSttBtm = nSavSttBtm;
262 0 : if( nBtmPos <= nTopEndPos )
263 : lcl_GCBorder_DelBorder( aBottom, --nSttBtm, false,
264 : *pTopLine, pBtmItem, nTopEndPos,
265 0 : pGCPara->pShareFmts );
266 : else
267 0 : nSttTop = nSavSttTop;
268 : }
269 0 : nTopPos = nBtmPos;
270 : }
271 :
272 0 : if( nTopPos == nBtmPos )
273 : {
274 0 : if( nSttBtm >= nEndBtm || nSttTop >= nEndTop )
275 : break;
276 :
277 0 : pBtmBox = &aBottom.GetBox( nSttBtm++, &nBtmPos );
278 0 : pTopBox = &aTop.GetBox( nSttTop++, &nTopPos );
279 0 : bGetTopItem = bGetBtmItem = true;
280 : }
281 0 : else if( nTopPos < nBtmPos )
282 : {
283 0 : if( nSttTop >= nEndTop )
284 0 : break;
285 0 : pTopBox = &aTop.GetBox( nSttTop++, &nTopPos );
286 0 : bGetTopItem = true;
287 0 : bGetBtmItem = false;
288 : }
289 : else
290 : {
291 0 : if( nSttBtm >= nEndBtm )
292 0 : break;
293 0 : pBtmBox = &aBottom.GetBox( nSttBtm++, &nBtmPos );
294 0 : bGetTopItem = false;
295 0 : bGetBtmItem = true;
296 : }
297 :
298 0 : } while( true );
299 : }
300 :
301 0 : for( SwTableBoxes::const_iterator it = pLine->GetTabBoxes().begin();
302 0 : it != pLine->GetTabBoxes().end(); ++it)
303 0 : lcl_GC_Box_Border(*it, pGCPara );
304 :
305 0 : ++pGCPara->nLinePos;
306 0 : }
307 :
308 0 : static void lcl_GC_Box_Border( const SwTableBox* pBox, _SwGCLineBorder* pPara )
309 : {
310 0 : if( !pBox->GetTabLines().empty() )
311 : {
312 0 : _SwGCLineBorder aPara( *pBox );
313 0 : aPara.pShareFmts = pPara->pShareFmts;
314 0 : BOOST_FOREACH( const SwTableLine* pLine, pBox->GetTabLines() )
315 0 : sw_GC_Line_Border( pLine, &aPara );
316 : }
317 0 : }
318 :
319 : struct _GCLinePara
320 : {
321 : SwTableLines* pLns;
322 : SwShareBoxFmts* pShareFmts;
323 :
324 1188 : _GCLinePara( SwTableLines& rLns, _GCLinePara* pPara = 0 )
325 1188 : : pLns( &rLns ), pShareFmts( pPara ? pPara->pShareFmts : 0 )
326 1188 : {}
327 : };
328 :
329 : static bool lcl_MergeGCLine(SwTableLine* pLine, _GCLinePara* pPara);
330 :
331 17014 : static bool lcl_MergeGCBox(SwTableBox* pTblBox, _GCLinePara* pPara)
332 : {
333 17014 : sal_uInt16 n, nLen = pTblBox->GetTabLines().size();
334 17014 : if( nLen )
335 : {
336 : // ATTENTION: The Line count can change!
337 0 : _GCLinePara aPara( pTblBox->GetTabLines(), pPara );
338 0 : for( n = 0; n < pTblBox->GetTabLines().size() &&
339 0 : lcl_MergeGCLine( pTblBox->GetTabLines()[n], &aPara );
340 : ++n )
341 : ;
342 :
343 0 : if( 1 == pTblBox->GetTabLines().size() )
344 : {
345 : // we have a box with a single line, so we just replace it by the line's boxes
346 0 : SwTableLine* pInsLine = pTblBox->GetUpper();
347 0 : SwTableLine* pCpyLine = pTblBox->GetTabLines()[0];
348 0 : SwTableBoxes::iterator it = std::find( pInsLine->GetTabBoxes().begin(), pInsLine->GetTabBoxes().end(), pTblBox );
349 0 : for( n = 0; n < pCpyLine->GetTabBoxes().size(); ++n )
350 0 : pCpyLine->GetTabBoxes()[n]->SetUpper( pInsLine );
351 :
352 : // remove the old box from its parent line
353 0 : it = pInsLine->GetTabBoxes().erase( it );
354 : // insert the nested line's boxes in its place
355 0 : pInsLine->GetTabBoxes().insert( it, pCpyLine->GetTabBoxes().begin(), pCpyLine->GetTabBoxes().end());
356 0 : pCpyLine->GetTabBoxes().clear();
357 : // destroy the removed box
358 0 : delete pTblBox;
359 :
360 0 : return false; // set up anew
361 : }
362 : }
363 17014 : return true;
364 : }
365 :
366 5898 : static bool lcl_MergeGCLine(SwTableLine* pLn, _GCLinePara* pGCPara)
367 : {
368 5898 : sal_uInt16 nLen = pLn->GetTabBoxes().size();
369 5898 : if( nLen )
370 : {
371 11796 : while( 1 == nLen )
372 : {
373 : // We have a Box with Lines
374 808 : SwTableBox* pBox = pLn->GetTabBoxes().front();
375 808 : if( pBox->GetTabLines().empty() )
376 808 : break;
377 :
378 0 : SwTableLine* pLine = pBox->GetTabLines()[0];
379 :
380 : // pLine turns into the current Line (that is rpLine), the rest is moved
381 : // into the LinesArray past the current one.
382 : // The LinesArray is in pPara!
383 0 : nLen = pBox->GetTabLines().size();
384 :
385 0 : SwTableLines& rLns = *pGCPara->pLns;
386 0 : sal_uInt16 nInsPos = rLns.GetPos( pLn );
387 : OSL_ENSURE( USHRT_MAX != nInsPos, "Could not find Line!" );
388 :
389 0 : SwTableBox* pUpper = pLn->GetUpper();
390 :
391 0 : rLns.erase( rLns.begin() + nInsPos ); // remove the Line from the array
392 0 : rLns.insert( rLns.begin() + nInsPos, pBox->GetTabLines().begin(), pBox->GetTabLines().end() );
393 :
394 : // JP 31.03.99: Bug 60000
395 : // Pass the attributes of the to-be-deleted Lines to the "inserted" one
396 : const SfxPoolItem* pItem;
397 0 : if( SfxItemState::SET == pLn->GetFrmFmt()->GetItemState(
398 0 : RES_BACKGROUND, true, &pItem ))
399 : {
400 0 : SwTableLines& rBoxLns = pBox->GetTabLines();
401 0 : for( sal_uInt16 nLns = 0; nLns < nLen; ++nLns )
402 0 : if( SfxItemState::SET != rBoxLns[ nLns ]->GetFrmFmt()->
403 0 : GetItemState( RES_BACKGROUND, true ))
404 0 : pGCPara->pShareFmts->SetAttr( *rBoxLns[ nLns ], *pItem );
405 : }
406 :
407 0 : pBox->GetTabLines().erase( pBox->GetTabLines().begin(), pBox->GetTabLines().begin() + nLen ); // Remove Lines from the array
408 :
409 0 : delete pLn;
410 :
411 : // Set the dependency anew
412 0 : while( nLen-- )
413 0 : rLns[ nInsPos++ ]->SetUpper( pUpper );
414 :
415 0 : pLn = pLine; // and set up anew
416 0 : nLen = pLn->GetTabBoxes().size();
417 : }
418 :
419 : // ATTENTION: The number of boxes can change!
420 22912 : for( nLen = 0; nLen < pLn->GetTabBoxes().size(); ++nLen )
421 17014 : if( !lcl_MergeGCBox( pLn->GetTabBoxes()[nLen], pGCPara ))
422 0 : --nLen;
423 : }
424 5898 : return true;
425 : }
426 :
427 : // Clean structure a bit
428 1188 : void SwTable::GCLines()
429 : {
430 : // ATTENTION: The Line count can change!
431 1188 : _GCLinePara aPara( GetTabLines() );
432 1188 : SwShareBoxFmts aShareFmts;
433 1188 : aPara.pShareFmts = &aShareFmts;
434 7086 : for( sal_uInt16 n = 0; n < GetTabLines().size() &&
435 5898 : lcl_MergeGCLine( GetTabLines()[n], &aPara ); ++n )
436 1188 : ;
437 1458 : }
438 :
439 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|