merge the formfield patch from ooo-build
[ooovba.git] / sw / source / filter / ww8 / WW8TableInfo.cxx
blob022b3d045b1263366168a96567356480fa7002af
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WW8TableInfo.cxx,v $
10 * $Revision: 1.1.2.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
34 #include <iostream>
35 #include <stdio.h>
36 #include "WW8TableInfo.hxx"
37 #include "swtable.hxx"
38 #include "pam.hxx"
39 #include "ndtxt.hxx"
40 #include "dbgoutsw.hxx"
42 namespace ww8
45 // WW8TableNodeInfoInner
47 WW8TableNodeInfoInner::WW8TableNodeInfoInner(WW8TableNodeInfo * pParent)
48 : mpParent(pParent)
49 , mnCell(0)
50 , mnRow(0)
51 , mbEndOfLine(false)
52 , mbEndOfCell(false)
53 , mpTableBox(NULL)
54 , mpTable(NULL)
58 WW8TableNodeInfoInner::~WW8TableNodeInfoInner()
62 void WW8TableNodeInfoInner::setDepth(sal_uInt32 nDepth)
64 mnDepth = nDepth;
67 void WW8TableNodeInfoInner::setCell(sal_uInt32 nCell)
69 mnCell = nCell;
72 void WW8TableNodeInfoInner::setRow(sal_uInt32 nRow)
74 mnRow = nRow;
77 void WW8TableNodeInfoInner::setEndOfLine(bool bEndOfLine)
79 mbEndOfLine = bEndOfLine;
82 void WW8TableNodeInfoInner::setEndOfCell(bool bEndOfCell)
84 mbEndOfCell = bEndOfCell;
87 void WW8TableNodeInfoInner::setTableBox(const SwTableBox * pTableBox)
89 mpTableBox = pTableBox;
92 void WW8TableNodeInfoInner::setTable(const SwTable * pTable)
94 mpTable = pTable;
97 sal_uInt32 WW8TableNodeInfoInner::getDepth() const
99 return mnDepth;
102 sal_uInt32 WW8TableNodeInfoInner::getCell() const
104 return mnCell;
107 sal_uInt32 WW8TableNodeInfoInner::getRow() const
109 return mnRow;
112 bool WW8TableNodeInfoInner::isEndOfCell() const
114 return mbEndOfCell;
117 bool WW8TableNodeInfoInner::isEndOfLine() const
119 return mbEndOfLine;
122 const SwNode * WW8TableNodeInfoInner::getNode() const
124 const SwNode * pResult = NULL;
126 if (mpParent != NULL)
127 pResult = mpParent->getNode();
129 return pResult;
132 const SwTableBox * WW8TableNodeInfoInner::getTableBox() const
134 return mpTableBox;
137 const SwTable * WW8TableNodeInfoInner::getTable() const
139 return mpTable;
142 string WW8TableNodeInfoInner::toString() const
144 static char buffer[256];
145 snprintf(buffer, sizeof(buffer),
146 "<tableinner depth=\"%" SAL_PRIxUINT32 "\""
147 " cell=\"%" SAL_PRIxUINT32 "\""
148 " row=\"%" SAL_PRIxUINT32 "\""
149 " endOfCell=\"%s\""
150 " endOfLine=\"%s\"/>",
151 mnDepth, mnCell, mnRow,
152 mbEndOfCell ? "yes" : "no",
153 mbEndOfLine ? "yes" : "no");
155 return string(buffer);
158 // WW8TableTextNodeInfo
160 WW8TableNodeInfo::WW8TableNodeInfo(const SwNode * pNode)
162 mnDepth(0),
163 mpNode(pNode),
164 mpNext(NULL)
168 WW8TableNodeInfo::~WW8TableNodeInfo()
172 ::std::string WW8TableNodeInfo::toString() const
174 static char buffer[1024];
175 snprintf(buffer, sizeof(buffer),
176 "<tableNodeInfo depth=\"%" SAL_PRIxUINT32 "\">"
177 , getDepth());
179 ::std::string sResult(buffer);
181 Inners_t::const_iterator aIt(mInners.begin());
182 Inners_t::const_iterator aEnd(mInners.end());
184 while (aIt != aEnd)
186 WW8TableNodeInfoInner::Pointer_t pInner = aIt->second;
187 sResult += pInner->toString();
189 aIt++;
192 #ifdef DEBUG
193 //!! does not compile with debug=t -> unresolved external (dbg_out),
194 //!! sommeone who knows what he wants to get should fix this
195 // sResult += dbg_out(*mpNode);
196 #endif
198 sResult +="</tableNodeInfo>";
200 return sResult;
203 void WW8TableNodeInfo::setDepth(sal_uInt32 nDepth)
205 mnDepth = nDepth;
207 Inners_t::iterator aIt = mInners.find(mnDepth);
209 if (aIt == mInners.end())
210 mInners[mnDepth] = WW8TableNodeInfoInner::Pointer_t(new WW8TableNodeInfoInner(this));
212 mInners[mnDepth]->setDepth(mnDepth);
215 void WW8TableNodeInfo::setEndOfLine(bool bEndOfLine)
217 WW8TableNodeInfoInner::Pointer_t pInner = getInnerForDepth(mnDepth);
218 pInner->setEndOfLine(bEndOfLine);
220 #ifdef DEBUG
221 ::std::clog << "<endOfLine depth=\"" << mnDepth << "\">"
222 << toString() << "</endOfLine>" << ::std::endl;
223 #endif
226 void WW8TableNodeInfo::setEndOfCell(bool bEndOfCell)
228 WW8TableNodeInfoInner::Pointer_t pInner = getInnerForDepth(mnDepth);
229 pInner->setEndOfCell(bEndOfCell);
231 #ifdef DEBUG
232 ::std::clog << "<endOfCell depth=\"" << mnDepth << "\">"
233 << toString() << "</endOfCell>" << ::std::endl;
234 #endif
237 void WW8TableNodeInfo::setTableBox(const SwTableBox * pTableBox)
239 getInnerForDepth(mnDepth)->setTableBox(pTableBox);
242 void WW8TableNodeInfo::setTable(const SwTable * pTable)
244 getInnerForDepth(mnDepth)->setTable(pTable);
247 void WW8TableNodeInfo::setNext(WW8TableNodeInfo * pNext)
249 mpNext = pNext;
252 void WW8TableNodeInfo::setCell(sal_uInt32 nCell)
254 getInnerForDepth(mnDepth)->setCell(nCell);
257 void WW8TableNodeInfo::setRow(sal_uInt32 nRow)
259 getInnerForDepth(mnDepth)->setRow(nRow);
262 sal_uInt32 WW8TableNodeInfo::getDepth() const
264 if (mInners.size() > 0)
265 return mInners.begin()->second->getDepth();
267 return mnDepth;
270 const SwNode * WW8TableNodeInfo::getNode() const
272 return mpNode;
275 const SwTableBox * WW8TableNodeInfo::getTableBox() const
277 return getInnerForDepth(mnDepth)->getTableBox();
280 const SwTable * WW8TableNodeInfo::getTable() const
282 return getInnerForDepth(mnDepth)->getTable();
285 WW8TableNodeInfo * WW8TableNodeInfo::getNext() const
287 return mpNext;
290 bool WW8TableNodeInfo::isEndOfLine() const
292 return getInnerForDepth(mnDepth)->isEndOfLine();
295 bool WW8TableNodeInfo::isEndOfCell() const
297 return getInnerForDepth(mnDepth)->isEndOfCell();
300 sal_uInt32 WW8TableNodeInfo::getCell() const
302 return getInnerForDepth(mnDepth)->getCell();
305 sal_uInt32 WW8TableNodeInfo::getRow() const
307 return getInnerForDepth(mnDepth)->getRow();
310 const ww8::WW8TableNodeInfo::Inners_t & WW8TableNodeInfo::getInners() const
312 return mInners;
315 const WW8TableNodeInfoInner::Pointer_t WW8TableNodeInfo::getFirstInner() const
317 WW8TableNodeInfoInner::Pointer_t pResult;
319 if (mInners.size() > 0)
320 pResult = mInners.begin()->second;
322 return pResult;
325 const WW8TableNodeInfoInner::Pointer_t WW8TableNodeInfo::getInnerForDepth(sal_uInt32 nDepth) const
327 WW8TableNodeInfoInner::Pointer_t pResult;
329 Inners_t::const_iterator aIt = mInners.find(nDepth);
330 if (aIt != mInners.end())
332 pResult = aIt->second;
335 return pResult;
338 // WW8TableInfo
340 WW8TableInfo::WW8TableInfo()
344 WW8TableInfo::~WW8TableInfo()
348 void WW8TableInfo::processSwTable(const SwTable * pTable)
350 #ifdef DEBUG
351 ::std::clog << "<processSwTable>" << ::std::endl;
352 #endif
354 const SwTableLines & rLines = pTable->GetTabLines();
356 WW8TableNodeInfo * pPrev = NULL;
358 for (USHORT n = 0; n < rLines.Count(); n++)
360 const SwTableLine * pLine = rLines[n];
362 pPrev = processTableLine(pTable, pLine, n, 1, pPrev);
365 #ifdef DEBUG
366 ::std::clog << "</processSwTable>" << ::std::endl;
367 #endif
370 WW8TableNodeInfo *
371 WW8TableInfo::processTableLine(const SwTable * pTable,
372 const SwTableLine * pTableLine,
373 sal_uInt32 nRow,
374 sal_uInt32 nDepth, WW8TableNodeInfo * pPrev)
376 #ifdef DEBUG
377 ::std::clog << "<processTableLine row=\"" << nRow << "\" depth=\""
378 << nDepth << "\">" << ::std::endl;
379 #endif
381 const SwTableBoxes & rBoxes = pTableLine->GetTabBoxes();
383 WW8TableNodeInfo::Pointer_t pTextNodeInfo;
385 for (USHORT n = 0; n < rBoxes.Count(); n++)
387 const SwTableBox * pBox = rBoxes[n];
389 pPrev = processTableBox(pTable, pBox, nRow, n, nDepth, n == rBoxes.Count() - 1, pPrev);
392 #ifdef DEBUG
393 ::std::clog << "</processTableLine>" << ::std::endl;
394 #endif
396 return pPrev;
399 WW8TableNodeInfo::Pointer_t
400 WW8TableInfo::processTableBoxLines(const SwTableBox * pBox,
401 const SwTable * pTable,
402 const SwTableBox * pBoxToSet,
403 sal_uInt32 nRow,
404 sal_uInt32 nCell,
405 sal_uInt32 nDepth)
407 #ifdef DEBUG
408 ::std::clog << "<processTableBoxLines depth=\"" << nDepth
409 << "\" row=\"" << nRow << "\" cell=\"" << nCell << "\">" << ::std::endl;
410 #endif
412 const SwTableLines & rLines = pBox->GetTabLines();
413 WW8TableNodeInfo::Pointer_t pNodeInfo;
415 if (rLines.Count() > 0)
417 for (sal_uInt32 n = 0; n < rLines.Count(); n++)
419 const SwTableLine * pLine = rLines[n];
420 const SwTableBoxes & rBoxes = pLine->GetTabBoxes();
422 for (USHORT nBox = 0; nBox < rBoxes.Count(); nBox++)
423 pNodeInfo = processTableBoxLines(rBoxes[nBox], pTable, pBoxToSet, nRow, nCell, nDepth);
426 else
428 const SwStartNode * pSttNd = pBox->GetSttNd();
429 const SwEndNode * pEndNd = pSttNd->EndOfSectionNode();
430 SwPaM aPaM(*pSttNd, 0);
431 SwPaM aEndPaM(*pEndNd, 0);
433 bool bDone = false;
434 while (!bDone)
436 SwNode & rNode = aPaM.GetPoint()->nNode.GetNode();
438 pNodeInfo = insertTableNodeInfo(&rNode, pTable, pBoxToSet, nRow, nCell, nDepth);
440 if (aPaM.GetPoint()->nNode == aEndPaM.GetPoint()->nNode)
441 bDone = true;
442 else
443 aPaM.GetPoint()->nNode++;
447 #ifdef DEBUG
448 ::std::clog << "</processTableBoxLines>" << ::std::endl;
449 #endif
451 return pNodeInfo;
455 WW8TableNodeInfo *
456 WW8TableInfo::processTableBox(const SwTable * pTable,
457 const SwTableBox * pBox,
458 sal_uInt32 nRow,
459 sal_uInt32 nCell,
460 sal_uInt32 nDepth,
461 bool bEndOfLine,
462 WW8TableNodeInfo * pPrev)
464 #ifdef DEBUG
465 ::std::clog << "<processTableBox row=\"" << nRow << "\" cell=\"" << nCell
466 << "\" depth=\"" << nDepth << "\">" << ::std::endl;
467 #endif
469 WW8TableNodeInfo::Pointer_t pNodeInfo;
470 const SwTableLines & rLines = pBox->GetTabLines();
471 const SwStartNode * pSttNd = pBox->GetSttNd();
472 WW8TableNodeInfo::Pointer_t pEndOfCellInfo;
474 if (rLines.Count() > 0)
476 pNodeInfo = processTableBoxLines(pBox, pTable, pBox, nRow, nCell, nDepth);
477 pNodeInfo->setEndOfCell(true);
478 if (bEndOfLine)
479 pNodeInfo->setEndOfLine(true);
481 for (sal_uInt32 n = 0; n < rLines.Count(); n++)
483 const SwTableLine * pLine = rLines[n];
485 pPrev = processTableLine(pTable, pLine, n, 1, pPrev);
488 else
490 SwPaM aPaM(*pSttNd, 0);
492 bool bDone = false;
493 sal_uInt32 nDepthInsideCell = 0;
497 SwNode & rNode = aPaM.GetPoint()->nNode.GetNode();
499 if (rNode.IsStartNode())
501 if (nDepthInsideCell > 0)
502 pEndOfCellInfo.reset();
504 nDepthInsideCell++;
507 pNodeInfo = insertTableNodeInfo(&rNode, pTable, pBox, nRow, nCell, nDepth);
509 if (pPrev != NULL)
510 pPrev->setNext(pNodeInfo.get());
512 pPrev = pNodeInfo.get();
514 if (nDepthInsideCell == 1 && rNode.IsTxtNode())
515 pEndOfCellInfo = pNodeInfo;
517 if (rNode.IsEndNode())
519 nDepthInsideCell--;
521 if (nDepthInsideCell == 0 && pEndOfCellInfo.get() == NULL)
522 pEndOfCellInfo = pNodeInfo;
524 SwEndNode * pEndNode = rNode.GetEndNode( );
525 SwStartNode * pTmpSttNd = pEndNode->StartOfSectionNode();
526 if (pTmpSttNd == pSttNd)
527 bDone = true;
530 aPaM.GetPoint()->nNode++;
532 while (!bDone);
534 if (pEndOfCellInfo.get() != NULL)
536 pEndOfCellInfo->setEndOfCell(true);
538 if (bEndOfLine)
539 pEndOfCellInfo->setEndOfLine(true);
543 #ifdef DEBUG
544 ::std::clog << "</processTableBox>" << ::std::endl;
545 #endif
547 return pPrev;
550 WW8TableNodeInfo::Pointer_t WW8TableInfo::insertTableNodeInfo
551 (const SwNode * pNode,
552 const SwTable * pTable,
553 const SwTableBox * pTableBox,
554 sal_uInt32 nRow,
555 sal_uInt32 nCell,
556 sal_uInt32 nDepth)
558 WW8TableNodeInfo::Pointer_t pNodeInfo = getTableNodeInfo(pNode);
560 if (pNodeInfo.get() == NULL)
562 pNodeInfo = WW8TableNodeInfo::Pointer_t(new WW8TableNodeInfo(pNode));
563 mMap.insert(Map_t::value_type(pNode, pNodeInfo));
566 pNodeInfo->setDepth(nDepth + pNodeInfo->getDepth());
568 pNodeInfo->setTable(pTable);
569 pNodeInfo->setTableBox(pTableBox);
571 pNodeInfo->setCell(nCell);
572 pNodeInfo->setRow(nRow);
574 #ifdef DEBUG
575 ::std::clog << pNodeInfo->toString() << ::std::endl;
576 #endif
578 return pNodeInfo;
581 WW8TableNodeInfo::Pointer_t WW8TableInfo::getTableNodeInfo
582 (const SwNode * pNode)
584 WW8TableNodeInfo::Pointer_t pResult;
585 Map_t::iterator aIt = mMap.find(pNode);
587 if (aIt != mMap.end())
588 pResult = (*aIt).second;
590 return pResult;
593 const SwNode * WW8TableInfo::getNextNode(const SwNode * pNode)
595 const SwNode * pResult = NULL;
597 WW8TableNodeInfo::Pointer_t pNodeInfo = getTableNodeInfo(pNode);
599 if (pNodeInfo.get() != NULL)
601 WW8TableNodeInfo * pNextInfo = pNodeInfo->getNext();
603 if (pNextInfo != NULL)
604 pResult = pNextInfo->getNode();
607 return pResult;