Bump version to 4.3-4
[LibreOffice.git] / lotuswordpro / source / filter / lwptablelayout.cxx
blob0fe5952518dad262633cf6f0a74440e6d9ca1e2b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * either of the following licenses
7 * - GNU Lesser General Public License Version 2.1
8 * - Sun Industry Standards Source License Version 1.1
10 * Sun Microsystems Inc., October, 2000
12 * GNU Lesser General Public License Version 2.1
13 * =============================================
14 * Copyright 2000 by Sun Microsystems, Inc.
15 * 901 San Antonio Road, Palo Alto, CA 94303, USA
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License version 2.1, as published by the Free Software Foundation.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
32 * Sun Industry Standards Source License Version 1.1
33 * =================================================
34 * The contents of this file are subject to the Sun Industry Standards
35 * Source License Version 1.1 (the "License"); You may not use this file
36 * except in compliance with the License. You may obtain a copy of the
37 * License at http://www.openoffice.org/license.html.
39 * Software provided under this License is provided on an "AS IS" basis,
40 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 * See the License for the specific provisions governing your rights and
44 * obligations concerning the Software.
46 * The Initial Developer of the Original Code is: IBM Corporation
48 * Copyright: 2008 by IBM Corporation
50 * All Rights Reserved.
52 * Contributor(s): _______________________________________
55 ************************************************************************/
56 /**
57 * @file
58 * For LWP filter architecture prototype - table layouts
60 /*************************************************************************
61 * Change History
62 Mar 2005 Created
63 ************************************************************************/
64 #include "lwpglobalmgr.hxx"
65 #include "lwptablelayout.hxx"
66 #include "lwpfoundry.hxx"
67 #include "lwpobjfactory.hxx"
68 #include "lwpholder.hxx"
69 #include "lwptable.hxx"
70 #include "lwptblcell.hxx"
71 #include "lwpnumericfmt.hxx"
72 #include "lwpdlvlist.hxx"
73 #include "lwppara.hxx"
75 #include "xfilter/xfstylemanager.hxx"
76 #include "xfilter/xftablestyle.hxx"
77 #include "xfilter/xftable.hxx"
78 #include "xfilter/xfrow.hxx"
79 #include "xfilter/xfrowstyle.hxx"
80 #include "xfilter/xfcell.hxx"
81 #include "xfilter/xfcellstyle.hxx"
82 #include "xfilter/xfcolstyle.hxx"
83 #include "xfilter/xfframestyle.hxx"
84 #include "xfilter/xfframe.hxx"
85 #include "xfilter/xffloatframe.hxx"
86 #include "lwpframelayout.hxx"
87 #include "xfilter/xfparastyle.hxx"
89 LwpSuperTableLayout::LwpSuperTableLayout(LwpObjectHeader &objHdr, LwpSvStream* pStrm)
90 : LwpPlacableLayout(objHdr, pStrm)
92 m_pFrame = new LwpFrame(this);
95 LwpSuperTableLayout::~LwpSuperTableLayout()
97 if(m_pFrame)
99 delete m_pFrame;
103 * @short Read super table layout record
105 void LwpSuperTableLayout::Read()
107 LwpPlacableLayout::Read();
108 m_pObjStrm->SkipExtra();
112 * @short Get child table layout
113 * @return pointer to table layout
115 LwpTableLayout* LwpSuperTableLayout::GetTableLayout()
117 LwpObjectID *pID = GetChildTail();
119 while(pID && !pID->IsNull())
121 LwpLayout* pLayout = dynamic_cast<LwpLayout*>(pID->obj());
122 if (!pLayout)
124 break;
126 if (pLayout->GetLayoutType() == LWP_TABLE_LAYOUT)
128 return dynamic_cast<LwpTableLayout *>(pLayout);
130 pID = pLayout->GetPrevious();
133 return NULL;
136 * @short Get effective heading table layout, the one just before table layout is the only one which is effective
137 * @return LwpTableHeadingLayout* - pointer to table heading layout
139 LwpTableHeadingLayout* LwpSuperTableLayout::GetTableHeadingLayout()
141 LwpObjectID *pID = GetChildTail();
143 while(pID && !pID->IsNull())
145 LwpLayout * pLayout = dynamic_cast<LwpLayout *>(pID->obj());
146 if (!pLayout)
148 break;
151 if (pLayout->GetLayoutType() == LWP_TABLE_HEADING_LAYOUT)
153 return dynamic_cast<LwpTableHeadingLayout *>(pLayout);
155 pID = pLayout->GetPrevious();
158 return NULL;
161 * @short Register super table layout style
163 void LwpSuperTableLayout::RegisterNewStyle()
165 // if this layout is style of real table entry
166 LwpTableLayout* pTableLayout = GetTableLayout();
167 if (pTableLayout != NULL)
169 pTableLayout->SetFoundry(m_pFoundry);
170 pTableLayout->RegisterStyle();
174 * @short Judge whether table size is according to content, borrowed from Word Pro code
175 * @param
176 * @return sal_Bool
178 bool LwpSuperTableLayout::IsSizeRightToContent()
180 /* Only "with paragraph above" tables can size right to content. */
181 if (GetRelativeType() == LwpLayoutRelativityGuts::LAY_INLINE_NEWLINE)
182 return LwpPlacableLayout::IsSizeRightToContent();
184 return false;
187 * @short Judge whether table is justifiable, borrowed from Word Pro code
188 * @param
189 * @return sal_Bool
191 bool LwpSuperTableLayout::IsJustifiable()
193 return (GetRelativeType() != LwpLayoutRelativityGuts::LAY_INLINE_NEWLINE || IsSizeRightToContent());
196 * @short Get width of frame outside table
197 * @param pTableStyle - pointer of XFTableStyle
198 * @return double - table width
200 double LwpSuperTableLayout::GetWidth()
202 double dWidth = GetTableWidth();
203 double dLeft = GetMarginsValue(MARGIN_LEFT);
204 double dRight = GetMarginsValue(MARGIN_RIGHT);
206 return (dWidth + dLeft + dRight);
209 * @short Get width of table
210 * @param pTableStyle - pointer of XFTableStyle
211 * @return double - table width
213 double LwpSuperTableLayout::GetTableWidth()
215 sal_Int32 nWidth = 0;
216 if(!IsJustifiable() || ((nWidth = LwpMiddleLayout::GetMinimumWidth()) <= 0))
218 LwpTableLayout* pTableLayout = GetTableLayout();
219 if(!pTableLayout)
221 assert(false);
222 return 0;
224 LwpTable *pTable = pTableLayout->GetTable();
225 if(!pTable)
227 assert(false);
228 return 0;
230 double dDefaultWidth = pTable->GetWidth();
231 sal_uInt16 nCol = pTable->GetColumn();
233 double dWidth = 0;
235 for(sal_uInt16 i =0; i< nCol; i++)
237 LwpObjectID *pColumnID = pTableLayout->GetColumnLayoutHead();
238 LwpColumnLayout * pColumnLayout = dynamic_cast<LwpColumnLayout *>(pColumnID->obj());
239 double dColumnWidth = dDefaultWidth;
240 while (pColumnLayout)
242 if(pColumnLayout->GetColumnID() == i)
244 dColumnWidth = pColumnLayout->GetWidth();
245 break;
247 pColumnID = pColumnLayout->GetNext();
248 pColumnLayout = dynamic_cast<LwpColumnLayout *>(pColumnID->obj());
250 dWidth += dColumnWidth;
253 return dWidth;
256 double dLeft = GetMarginsValue(MARGIN_LEFT);
257 double dRight = GetMarginsValue(MARGIN_RIGHT);
258 return LwpTools::ConvertFromUnitsToMetric(nWidth)-dLeft-dRight;
262 * @short Apply shadow to table
263 * @param pTableStyle - pointer of XFTableStyle
264 * @return
266 void LwpSuperTableLayout::ApplyShadow(XFTableStyle *pTableStyle)
268 // use shadow property of supertable
269 boost::scoped_ptr<XFShadow> pXFShadow(GetXFShadow());
270 if(pXFShadow)
272 pTableStyle->SetShadow(pXFShadow->GetPosition(), pXFShadow->GetOffset(), pXFShadow->GetColor());
276 * @short Apply pattern fill to table style
277 * @param pTableStyle - pointer of XFTableStyle
278 * @return
280 void LwpSuperTableLayout::ApplyPatternFill(XFTableStyle* pTableStyle)
282 XFBGImage* pXFBGImage = this->GetFillPattern();
283 if (pXFBGImage)
285 pTableStyle->SetBackImage(pXFBGImage);
290 * @short Apply background to table style
291 * @param pTableStyle - pointer of XFTableStyle
292 * @return
294 void LwpSuperTableLayout::ApplyBackGround(XFTableStyle* pTableStyle)
296 if (this->IsPatternFill())
298 ApplyPatternFill(pTableStyle);
300 else
302 ApplyBackColor(pTableStyle);
306 * @short Apply back color to table
307 * @param pTableStyle - pointer of XFTableStyle
308 * @return
310 void LwpSuperTableLayout::ApplyBackColor(XFTableStyle *pTableStyle)
312 LwpColor* pColor = GetBackColor();
313 if(pColor && pColor->IsValidColor())
315 XFColor aColor(pColor->To24Color());
316 pTableStyle->SetBackColor(aColor);
320 * @short Apply watermark to table
321 * @param pTableStyle - pointer of XFTableStyle
322 * @return
324 void LwpSuperTableLayout::ApplyWatermark(XFTableStyle *pTableStyle)
326 XFBGImage* pBGImage = GetXFBGImage();
327 if(pBGImage)
329 pTableStyle->SetBackImage(pBGImage);
333 * @short Apply alignment to table
334 * @param pTableStyle - pointer of XFTableStyle
335 * @return
337 void LwpSuperTableLayout::ApplyAlignment(XFTableStyle * pTableStyle)
339 LwpPoint aPoint;
340 if (GetGeometry())
341 aPoint = GetGeometry()->GetOrigin();
342 //LwpPoint aPoint = GetOrigin();
343 double dXOffset = LwpTools::ConvertFromUnitsToMetric(aPoint.GetX());
345 // add left padding to alignment distance
346 double dLeft = GetMarginsValue(MARGIN_LEFT);
348 pTableStyle->SetAlign(enumXFAlignStart, dXOffset+ dLeft);
351 * @short Add table to container
352 * @param pCont - pointer of container
353 * @return pCont
355 void LwpSuperTableLayout::XFConvert(XFContentContainer* pCont)
357 if ( LwpLayoutRelativityGuts::LAY_INLINE_NEWLINE == GetRelativeType()
358 && !GetContainerLayout()->IsCell())
360 LwpTableLayout * pTableLayout = GetTableLayout();
361 if (pTableLayout)
363 pTableLayout->XFConvert(pCont);
366 else if(IsRelativeAnchored())
368 //anchor to paragraph except "with paragraph above"
369 XFConvertFrame(pCont);
371 else if(m_pFrame)
373 //anchor to page, frame, cell
374 m_pFrame->XFConvert(pCont);
378 * @short convert frame which anchor to page
379 * @param
380 * @return
382 void LwpSuperTableLayout::XFConvertFrame(XFContentContainer* pCont, sal_Int32 nStart, sal_Int32 nEnd, bool bAll)
384 if(m_pFrame)
386 XFFrame* pXFFrame = NULL;
387 if(nEnd < nStart)
389 pXFFrame = new XFFrame();
391 else
393 pXFFrame = new XFFloatFrame(nStart, nEnd, bAll);
396 m_pFrame->Parse(pXFFrame, static_cast<sal_uInt16>(nStart));
397 //parse table, and add table to frame
398 LwpTableLayout * pTableLayout = GetTableLayout();
399 if (pTableLayout)
401 pTableLayout->XFConvert(pXFFrame);
403 //add frame to the container
404 pCont ->Add(pXFFrame);
409 * @short register frame style
410 * @param
411 * @return
413 void LwpSuperTableLayout::RegisterFrameStyle()
415 XFFrameStyle* pFrameStyle = new XFFrameStyle();
416 m_pFrame->RegisterStyle(pFrameStyle);
419 LwpTableLayout::LwpTableLayout(LwpObjectHeader &objHdr, LwpSvStream* pStrm)
420 : LwpLayout(objHdr, pStrm)
421 , m_nRows(0)
422 , m_nCols(0)
423 , m_pDefaultCellLayout(NULL)
424 , m_pColumns(NULL)
425 , m_pXFTable(NULL)
427 m_CellsMap.clear();
430 LwpTableLayout::~LwpTableLayout()
432 m_CellsMap.clear();
434 if (m_pColumns)
436 delete [] m_pColumns;
437 m_pColumns = NULL;
442 * @short Get neighbour cell by specifying ROW+COL
443 * @param nRow
444 * @param nCol
445 * @return LwpCellLayout *
447 LwpCellLayout * LwpTableLayout::GetCellByRowCol(sal_uInt16 nRow, sal_uInt16 nCol)
449 if (nRow >= m_nRows || nCol >= m_nCols)
450 return NULL;
452 return m_WordProCellsMap[nRow*m_nCols + nCol];
455 * @short traverse all table cells
456 * @param
457 * @param
458 * @param
460 void LwpTableLayout::TraverseTable()
462 sal_uInt32 nCount = m_nRows*m_nCols;
464 // new cell map nRow*nCOl and initialize
465 for (sal_uInt32 iLoop = 0; iLoop < nCount; ++iLoop)
467 m_WordProCellsMap.push_back(GetDefaultCellLayout());
470 // set value
471 LwpObjectID *pRowID = GetChildHead();
472 LwpRowLayout * pRowLayout = dynamic_cast<LwpRowLayout *>(pRowID->obj());
473 while (pRowLayout)
475 pRowLayout->SetRowMap();
477 // for 's analysis job
478 m_RowsMap[pRowLayout->GetRowID()] = pRowLayout;
479 pRowLayout->CollectMergeInfo();
480 // end for 's analysis
482 pRowID = pRowLayout->GetNext();
483 pRowLayout = dynamic_cast<LwpRowLayout *>(pRowID->obj());
488 * @short search the cell map
489 * @param nRow - row id (0 based)
490 * @param nRow - row id (0 based)
491 * @return LwpObjectID * - pointer to cell story object ID
493 LwpObjectID * LwpTableLayout::SearchCellStoryMap(sal_uInt16 nRow, sal_uInt16 nCol)
495 if (nRow >= m_nRows || nCol >= m_nCols )
497 return NULL;
500 LwpCellLayout * pCell = GetCellByRowCol(nRow, nCol);
501 if (pCell)
503 // maybe connected cell layout
504 // maybe default cell layout
505 if (nRow != pCell->GetRowID() || nCol != pCell->GetColID())
507 return NULL;
509 return pCell->GetContent();
512 return NULL;
516 * @short Get parent super table layout of table layout
517 * @return LwpSuperTableLayout * - pointer of parent super table layout
519 LwpSuperTableLayout * LwpTableLayout::GetSuperTableLayout()
521 return dynamic_cast<LwpSuperTableLayout *>(GetParent()->obj());
524 * @short Get table pointer
525 * @return LwpTable * - content table pointer
527 LwpTable * LwpTableLayout::GetTable()
529 LwpTable *pTable = dynamic_cast<LwpTable *>(m_Content.obj());
530 return pTable;
533 * @short Get column style name by column ID
534 * @param sal_uInt16 -- col id(0 based)
535 * @return OUString - name of column style
537 OUString LwpTableLayout::GetColumnWidth(sal_uInt16 nCol)
539 if (nCol >= m_nCols)
541 assert(false);
542 return m_DefaultColumnStyleName;
545 LwpColumnLayout * pCol = m_pColumns[nCol];
546 if (pCol)
548 return pCol->GetStyleName();
551 return m_DefaultColumnStyleName;
554 * @short analyze all columns to get whole table width and width of all columns
555 * @short and register all column styles
556 * @param none
558 void LwpTableLayout::RegisterColumns()
560 LwpTable * pTable = GetTable();
561 LwpSuperTableLayout * pSuper = GetSuperTableLayout();
563 sal_uInt16 nCols = m_nCols;
565 m_pColumns = new LwpColumnLayout *[nCols];
566 sal_Bool * pWidthCalculated = new sal_Bool[nCols];
567 for(sal_uInt16 i=0;i<nCols; i++)
569 pWidthCalculated[i] = sal_False;
570 m_pColumns[i] = NULL;
573 double dDefaultColumn = pTable->GetWidth();
574 sal_uInt16 nJustifiableColumn = nCols;
576 double dTableWidth = pSuper->GetTableWidth();
578 // Get total width of justifiable columns
579 // NOTICE: all default columns are regarded as justifiable columns
580 LwpObjectID *pColumnID = GetColumnLayoutHead();
581 LwpColumnLayout * pColumnLayout = dynamic_cast<LwpColumnLayout *>(pColumnID->obj());
582 while (pColumnLayout)
584 m_pColumns[pColumnLayout->GetColumnID()] = pColumnLayout;
585 if (!pColumnLayout->IsJustifiable())
587 pWidthCalculated[pColumnLayout->GetColumnID()] = sal_True;
588 dTableWidth -= pColumnLayout->GetWidth();
589 nJustifiableColumn --;
592 pColumnID = pColumnLayout->GetNext();
593 pColumnLayout = dynamic_cast<LwpColumnLayout *>(pColumnID->obj());
596 // if all columns are not justifiable, the rightmost column will be changed to justifiable
597 if(nJustifiableColumn == 0)
599 nJustifiableColumn ++;
600 if (m_pColumns[nCols - 1])
602 pWidthCalculated[nCols-1] = sal_False;
603 dTableWidth += m_pColumns[nCols-1]->GetWidth();
605 else
607 // this can't happen
608 dTableWidth = dDefaultColumn;
609 assert(false);
613 // justifiable columns will share the remain width averagely
614 dDefaultColumn = dTableWidth/nJustifiableColumn;
616 // register default column style
617 XFColStyle *pColStyle = new XFColStyle();
618 pColStyle->SetWidth(static_cast<float>(dDefaultColumn));
620 XFStyleManager* pXFStyleManager = LwpGlobalMgr::GetInstance()->GetXFStyleManager();
621 m_DefaultColumnStyleName = pXFStyleManager->AddStyle(pColStyle)->GetStyleName();
623 // register existed column style
624 sal_uInt16 i=0;
625 for( i=0;i<nCols; i++)
627 if(m_pColumns[i])
629 m_pColumns[i]->SetFoundry(m_pFoundry);
630 if(!pWidthCalculated[i])
632 // justifiable ----register style with calculated value
633 m_pColumns[i]->SetStyleName(m_DefaultColumnStyleName);
635 else
637 // not justifiable ---- register style with original value
638 m_pColumns[i]->RegisterStyle(m_pColumns[i]->GetWidth());
642 delete [] pWidthCalculated;
645 * @short register all row styles
646 * @param none
648 void LwpTableLayout::RegisterRows()
650 LwpTable * pTable = GetTable();
651 if (pTable == NULL)
653 assert(false);
654 return;
657 // register default row style
658 XFRowStyle * pRowStyle = new XFRowStyle();
659 if (m_nDirection & 0x0030)
661 pRowStyle->SetMinRowHeight((float)pTable->GetHeight());
663 else
665 pRowStyle->SetRowHeight((float)pTable->GetHeight());
667 XFStyleManager* pXFStyleManager = LwpGlobalMgr::GetInstance()->GetXFStyleManager();
668 m_DefaultRowStyleName = pXFStyleManager->AddStyle(pRowStyle)->GetStyleName();
670 // register style of rows
671 LwpObjectID * pRowID = GetChildHead();
672 LwpRowLayout * pRowLayout = dynamic_cast<LwpRowLayout *>(pRowID->obj());
673 while (pRowLayout)
675 pRowLayout->SetFoundry(m_pFoundry);
676 pRowLayout->RegisterStyle();
678 pRowID = pRowLayout->GetNext();
679 pRowLayout = dynamic_cast<LwpRowLayout *>(pRowID->obj());
683 * @short register table style, if needed, including frame style
684 * @param none
686 void LwpTableLayout::RegisterStyle()
688 // get super table layout
689 LwpSuperTableLayout * pSuper = GetSuperTableLayout();
690 if(!pSuper)
692 assert(false);
693 return;
696 // get table
697 LwpTable * pTable = GetTable();
698 if (pTable == NULL)
700 assert(false);
701 return;
704 // get row/column number of this table
705 m_nRows = pTable->GetRow();
706 m_nCols = pTable->GetColumn();
708 // get default cell layout of current table
709 LwpObjectID * pID= pTable->GetDefaultCellStyle();
710 if (pID)
712 m_pDefaultCellLayout = dynamic_cast<LwpCellLayout *>(pID->obj());
715 // register columns styles
716 RegisterColumns();
718 // register style of whole table
719 XFTableStyle * pTableStyle = new XFTableStyle();
721 sal_uInt8 nType = pSuper->GetRelativeType();
722 // If the table is not "with paragraph above" placement, create an frame style
723 // by supertable layout
724 if ( LwpLayoutRelativityGuts::LAY_INLINE_NEWLINE == nType
725 && !pSuper->GetContainerLayout()->IsCell())
727 //with para above
728 // pSuper->ApplyBackColor(pTableStyle);
729 pSuper->ApplyBackGround(pTableStyle);
730 pSuper->ApplyWatermark(pTableStyle);
731 pSuper->ApplyShadow(pTableStyle);
732 pSuper->ApplyAlignment(pTableStyle);
733 pTableStyle->SetWidth(pSuper->GetTableWidth());
735 else
737 pSuper->RegisterFrameStyle();
738 pTableStyle->SetAlign(enumXFAlignCenter);
739 pTableStyle->SetWidth(pSuper->GetTableWidth());
741 XFStyleManager* pXFStyleManager = LwpGlobalMgr::GetInstance()->GetXFStyleManager();
742 m_StyleName = pXFStyleManager->AddStyle(pTableStyle)->GetStyleName();
744 //convert to OO table now and register row stle
745 // traverse
746 TraverseTable();
748 SplitConflictCells();
750 // Register rows layouts, it must be after SplitConflictCells
751 RegisterRows();
753 // Parse table
754 ParseTable();
756 //Comment:The old code doesn't check if the LwpFoundry pointer is NULL,
757 // So the NULL pointer cause sodc frozee. Add code to check the
758 // the pointer.
759 //New Code
760 if( GetFoundry() && GetTable() )
762 PutCellVals( GetFoundry(),*GetTable()->GetObjectID() );
765 * @short read table layout
766 * @param none
768 void LwpTableLayout::ParseTable()
770 // get super table layout
771 LwpSuperTableLayout * pSuper = GetSuperTableLayout();
772 if(!pSuper)
774 assert(false);
775 return;
778 // set name of object
779 m_pXFTable = new XFTable;
780 m_pXFTable->SetTableName(pSuper->GetName()->str());
781 // set table style
782 m_pXFTable->SetStyleName(m_StyleName);
784 sal_uInt16 nRow = m_nRows;
785 sal_uInt8 nCol = (sal_uInt8)m_nCols;
787 //process header rows
788 LwpTableHeadingLayout* pTableHeading;
789 pTableHeading = pSuper->GetTableHeadingLayout();
790 sal_uInt16 nStartHeadRow;
791 sal_uInt16 nEndHeadRow;
792 sal_uInt16 nContentRow;
793 if (pTableHeading)
795 pTableHeading->GetStartEndRow(nStartHeadRow,nEndHeadRow);
796 if (nStartHeadRow != 0)
797 ConvertTable(m_pXFTable,0,nRow,0,nCol);
798 else
800 nContentRow = ConvertHeadingRow(m_pXFTable,nStartHeadRow,nEndHeadRow+1);
801 ConvertTable(m_pXFTable,nContentRow,nRow,0,nCol);
804 else
805 ConvertTable(m_pXFTable,0,nRow,0,nCol);
809 * @short read table layout
810 * @param none
812 void LwpTableLayout::Read()
814 LwpLayout::Read();
816 // before layout hierarchy rework!
817 if(LwpFileHeader::m_nFileRevision < 0x000b)
819 assert(false);
821 m_ColumnLayout.ReadIndexed(m_pObjStrm);
823 m_pObjStrm->SkipExtra();
827 * @short Convert table
828 * @param
829 * @return pCont - container which will contain table
831 void LwpTableLayout::XFConvert(XFContentContainer* pCont)
834 pCont->Add(m_pXFTable);
837 * @short convert heading row
838 * @param pXFTable - pointer of table
839 * @param nStartRow - start heading row ID
840 * @param nEndRow - end heading row ID
842 sal_uInt16 LwpTableLayout::ConvertHeadingRow(
843 XFTable* pXFTable,sal_uInt16 nStartHeadRow,sal_uInt16 nEndHeadRow)
845 sal_uInt16 nContentRow;
846 sal_uInt8 nCol = static_cast<sal_uInt8>(GetTable()->GetColumn());
847 XFTable* pTmpTable = new XFTable;
848 XFRow* pXFRow;
850 ConvertTable(pTmpTable,nStartHeadRow,nEndHeadRow,0,nCol);
852 sal_uInt16 nRowNum = pTmpTable->GetRowCount();
853 sal_uInt8* CellMark = new sal_uInt8[nRowNum];
854 bool bFindFlag = false;
856 if (nRowNum == 1)
858 pXFRow = pTmpTable->GetRow(1);
859 pXFTable->AddHeaderRow(pXFRow);
860 pTmpTable->RemoveRow(1);
861 nContentRow = nEndHeadRow;
863 else
865 sal_uInt8 nFirstColSpann = 1;
866 bFindFlag = FindSplitColMark(pTmpTable,CellMark,nFirstColSpann);
868 if (bFindFlag)//split to 2 cells
870 SplitRowToCells(pTmpTable,pXFTable,nFirstColSpann,CellMark);
871 nContentRow = nEndHeadRow;
873 else//can not split,the first row will be the heading row,the rest will be content row
875 pXFRow = pTmpTable->GetRow(1);
876 pXFTable->AddHeaderRow(pXFRow);
877 pTmpTable->RemoveRow(1);
878 nContentRow = m_RowsMap[0]->GetCurMaxSpannedRows(0,nCol);
881 delete pTmpTable;
882 delete [] CellMark;
883 return nContentRow;
886 void LwpTableLayout::SplitRowToCells(XFTable* pTmpTable,XFTable* pXFTable,
887 sal_uInt8 nFirstColSpann,sal_uInt8* pCellMark)
889 sal_uInt16 i;
890 sal_uInt8 j;
891 sal_uInt16 nRowNum = pTmpTable->GetRowCount();
892 sal_uInt8 nCol = static_cast<sal_uInt8>(GetTable()->GetColumn());
894 XFRow* pXFRow = new XFRow;
896 //register style for heading row
897 double fHeight = 0;
898 OUString styleName;
899 XFRowStyle* pRowStyle = new XFRowStyle;
900 styleName = pTmpTable->GetRow(1)->GetStyleName();
902 // get settings of the row and assign them to new row style
903 XFStyleManager* pXFStyleManager = LwpGlobalMgr::GetInstance()->GetXFStyleManager();
904 XFRowStyle *pTempRowStyle = static_cast<XFRowStyle*>(pXFStyleManager->FindStyle(styleName));
905 if (pTempRowStyle)
906 *pRowStyle = *pTempRowStyle;
908 for (i=1;i<=nRowNum;i++)
910 styleName = pTmpTable->GetRow(i)->GetStyleName();
911 fHeight+=static_cast<XFRowStyle*>(pXFStyleManager->FindStyle(styleName))->GetRowHeight();
913 if (m_nDirection & 0x0030)
915 pRowStyle->SetMinRowHeight((float)fHeight);
917 else
919 pRowStyle->SetRowHeight((float)fHeight);
921 pXFRow->SetStyleName(pXFStyleManager->AddStyle(pRowStyle)->GetStyleName());
923 //construct headong row
924 XFCell* pXFCell1 = new XFCell;
925 XFCell* pXFCell2 = new XFCell;
926 XFTable* pSubTable1 = new XFTable;
927 XFTable* pSubTable2 = new XFTable;
928 XFRow* pNewRow;
929 XFRow* pOldRow;
930 XFCell* pNewCell;
932 for (i=1;i<=nRowNum;i++)
934 pOldRow = pTmpTable->GetRow(i);
935 pNewRow = new XFRow;
936 pNewRow->SetStyleName(pOldRow->GetStyleName());
937 for (j=1;j<=pCellMark[i];j++)
939 pNewCell = pOldRow->GetCell(j);
940 pNewRow->AddCell(pNewCell);
942 pSubTable1->AddRow(pNewRow);
944 ConvertColumn(pSubTable1,0,nFirstColSpann);//add column info
946 pXFCell1->Add(pSubTable1);
947 pXFCell1->SetColumnSpaned(nFirstColSpann);
948 pXFRow->AddCell(pXFCell1);
950 for (i=1;i<=nRowNum;i++)
952 pOldRow = pTmpTable->GetRow(i);
953 pNewRow = new XFRow;
954 pNewRow->SetStyleName(pOldRow->GetStyleName());
955 for(j=pCellMark[i]+1;j<=pOldRow->GetCellCount();j++)
957 pNewCell = pOldRow->GetCell(j);
958 pNewRow->AddCell(pNewCell);
960 pSubTable2->AddRow(pNewRow);
963 ConvertColumn(pSubTable2,nFirstColSpann,nCol);//add column info
964 pXFCell2->Add(pSubTable2);
965 pXFCell2->SetColumnSpaned(nCol-nFirstColSpann);
966 pXFRow->AddCell(pXFCell2);
968 pXFTable->AddHeaderRow(pXFRow);
970 //remove tmp table
971 for (i=1;i<=nRowNum;i++)
973 pOldRow = pTmpTable->GetRow(i);
974 for(j=1;j<=pOldRow->GetCellCount();j++)
975 pOldRow->RemoveCell(j);
976 pTmpTable->RemoveRow(i);
981 * @short find if the heading rows can be split to 2 cells
982 * @param pXFTable - pointer of tmp XFtable
983 * @param CellMark - pointer of cell mark array
985 bool LwpTableLayout::FindSplitColMark(XFTable* pXFTable, sal_uInt8* pCellMark,
986 sal_uInt8& nMaxColSpan)
988 sal_uInt16 nRowNum = pXFTable->GetRowCount();
989 sal_uInt8 nColNum = static_cast<sal_uInt8>(pXFTable->GetColumnCount());
990 sal_uInt8 nCellMark=0;
991 sal_uInt8 nCount;
992 sal_uInt8 nColSpan;
993 bool bFindFlag = false;
994 XFRow* pTmpRow;
996 for(sal_uInt8 i=1;i<=nColNum;i++)
998 sal_uInt16 nRowLoop;
999 sal_uInt8 nCellLoop;
1001 //find current max column span
1002 nMaxColSpan = 1;
1003 for (nRowLoop=1;nRowLoop<=nRowNum;nRowLoop++)
1005 nColSpan = 0;
1006 for(nCellLoop=1; nCellLoop<i+1; nCellLoop++)
1008 pTmpRow = pXFTable->GetRow(nRowLoop);
1009 XFCell* pCell = pTmpRow->GetCell(nCellLoop);
1010 if (pCell)
1011 nColSpan += static_cast<sal_uInt8>(pCell->GetColSpaned());
1012 else
1013 return false;
1015 if (nColSpan > nMaxColSpan)
1016 nMaxColSpan = nColSpan;
1017 pCellMark[nRowLoop] = 0;//reset all cell mark to zero
1020 //find if other row has the same column
1021 for (nRowLoop=1;nRowLoop<=nRowNum;nRowLoop++)
1023 pTmpRow = pXFTable->GetRow(nRowLoop);
1024 nCount = 0;
1025 nCellMark = 0;
1026 for (nCellLoop=1; nCellLoop<=pTmpRow->GetCellCount(); nCellLoop++)
1028 if (nCount>nMaxColSpan)
1029 break;
1030 nCount+= static_cast<sal_uInt8>(pTmpRow->GetCell(nCellLoop)->GetColSpaned());
1031 if (nCount == nMaxColSpan)
1033 nCellMark = nCellLoop;
1034 break;
1037 if (nCellMark == 0)
1038 break;
1039 else
1040 pCellMark[nRowLoop] = nCellMark;
1042 for(nRowLoop=1;nRowLoop<=nRowNum;nRowLoop++)//check if all ==0,break
1044 if (pCellMark[nRowLoop] == 0)
1045 break;
1047 if (nRowLoop == nRowNum+1)
1049 bFindFlag = true;
1050 return bFindFlag;
1054 return bFindFlag;
1058 * @short convert word pro table to SODC table
1059 * @param pXFTable - pointer of table
1060 * @param nStartRow - start row ID
1061 * @param nEndRow - end row ID
1062 * @param nStartCol - start column ID
1063 * @param nEndCol - end column ID
1065 void LwpTableLayout::ConvertTable(XFTable* pXFTable,sal_uInt16 nStartRow,
1066 sal_uInt16 nEndRow,sal_uInt8 nStartCol,sal_uInt8 nEndCol)
1068 //out put column info TO BE CHANGED
1069 ConvertColumn(pXFTable,nStartCol,nEndCol);
1071 std::map<sal_uInt16,LwpRowLayout*>::iterator iter;
1073 for (sal_uInt16 i=nStartRow; i<nEndRow;)
1075 iter = m_RowsMap.find(i);
1076 if (iter == m_RowsMap.end())
1078 ConvertDefaultRow(pXFTable,nStartCol,nEndCol,i);
1079 i++;
1081 else
1083 LwpRowLayout* pRow = iter->second;
1084 if (pRow->GetCurMaxSpannedRows(nStartCol,nEndCol) == 1)
1086 pRow->ConvertCommonRow(pXFTable,nStartCol,nEndCol);
1087 i++;
1089 else
1091 pRow->ConvertRow(pXFTable,nStartCol,nEndCol);
1092 i += pRow->GetCurMaxSpannedRows(nStartCol,nEndCol);
1099 * @short apply numeric value and formula to cell
1100 * @param pFoundry - pointer of foundry
1101 * @param aTableID - table ID
1103 void LwpTableLayout::PutCellVals(LwpFoundry* pFoundry, LwpObjectID aTableID)
1106 //Comment:The old code doesn't check if the LwpFoundry pointer is NULL,
1107 // So the NULL pointer cause sodc frozee. Add code to check the
1108 // the pointer.
1109 //New Code
1110 if( !pFoundry ) return;
1112 try{
1114 LwpDLVListHeadHolder* pHolder = (LwpDLVListHeadHolder*)pFoundry->GetNumberManager()->GetTableRangeID().obj();
1116 LwpTableRange* pTableRange = (LwpTableRange*)pHolder->GetHeadID()->obj();
1118 //Look up the table
1119 while (NULL!=pTableRange)
1121 LwpObjectID aID = pTableRange->GetTableID();
1122 if (aID == aTableID)
1124 break;
1126 pTableRange = pTableRange->GetNext();
1129 if (pTableRange)
1131 LwpCellRange* pRange = (LwpCellRange*)pTableRange->GetCellRangeID().obj();
1132 LwpFolder* pFolder = (LwpFolder*)pRange->GetFolderID().obj();
1133 LwpObjectID aRowListID = pFolder->GetChildHeadID();
1134 LwpRowList* pRowList = (LwpRowList*)aRowListID.obj();
1136 //loop the rowlist
1137 while( NULL!=pRowList)
1139 sal_uInt16 nRowID = pRowList->GetRowID();
1141 LwpCellList* pCellList = (LwpCellList*)pRowList->GetChildHeadID().obj();
1142 //loop the celllist
1143 while( NULL!=pCellList)
1145 {//put cell
1146 sal_uInt16 nColID = pCellList->GetColumnID();
1148 XFCell* pCell = GetCellsMap(nRowID,static_cast<sal_uInt8>(nColID));
1149 if (pCell)
1151 pCellList->Convert(pCell, this);
1153 //process paragraph
1154 PostProcessParagraph(pCell, nRowID, nColID);
1156 else
1158 //Hidden cell would not be in cellsmap
1159 assert(false);
1162 pCellList = (LwpCellList*)pCellList->GetNextID().obj();
1165 pRowList =(LwpRowList*)pRowList->GetNextID().obj();
1169 }catch (...) {
1170 assert(false);
1175 * @short 1. set number right alignment to right if number 2. remove tab added before if number
1176 * @param pCell - cell which to be process
1177 * @param nRowID - row number in Word Pro file
1178 * @param nColID - column number in Word Pro file
1180 void LwpTableLayout::PostProcessParagraph(XFCell *pCell, sal_uInt16 nRowID, sal_uInt16 nColID)
1182 // if number right, set alignment to right
1183 LwpCellLayout * pCellLayout = GetCellByRowCol(nRowID, nColID);
1184 if(pCellLayout)
1186 XFParagraph * pXFPara = NULL;
1187 pXFPara = static_cast<XFParagraph*>(pCell->FindFirstContent(enumXFContentPara));
1188 if (!pXFPara)
1189 return;
1190 XFColor aNullColor = XFColor();
1192 if ( pXFPara)
1194 OUString sNumfmt = pCellLayout->GetNumfmtName();
1195 bool bColorMod = false;
1196 XFNumberStyle* pNumStyle = NULL;
1197 XFStyleManager* pXFStyleManager = LwpGlobalMgr::GetInstance()->GetXFStyleManager();
1198 if (!sNumfmt.isEmpty())
1200 pNumStyle = (XFNumberStyle*)pXFStyleManager->FindStyle( sNumfmt);
1201 XFColor aColor = pNumStyle->GetColor();
1202 if ( aColor != aNullColor )
1203 bColorMod = true;//end
1206 XFParaStyle * pStyle = pXFStyleManager->FindParaStyle(pXFPara->GetStyleName());
1207 if (pStyle->GetNumberRight() || bColorMod)
1209 XFParaStyle* pOverStyle = new XFParaStyle;
1210 *pOverStyle = *pStyle;
1212 if (pStyle->GetNumberRight())
1213 pOverStyle->SetAlignType(enumXFAlignEnd);
1215 if (bColorMod)
1217 XFFont* pFont = pOverStyle->GetFont();
1218 XFColor aColor = pFont->GetColor();
1219 if ( aColor == aNullColor )
1221 XFFont* pNewFont = new XFFont;
1222 aColor = pNumStyle->GetColor();
1223 pNewFont->SetColor(aColor);
1224 pOverStyle->SetFont(pNewFont);
1228 pOverStyle->SetStyleName("");
1229 OUString StyleName = pXFStyleManager->AddStyle(pOverStyle)->GetStyleName();
1231 pXFPara->SetStyleName(StyleName);
1238 * @short Parse all cols of table
1239 * @param pXFTable - pointer to created XFTable
1241 void LwpTableLayout::ConvertColumn(XFTable *pXFTable,sal_uInt8 nStartCol,sal_uInt8 nEndCol)
1243 LwpTable * pTable = GetTable();
1244 if (!pTable)
1246 assert(false);
1247 return;
1250 for (sal_uInt32 iLoop = 0; iLoop < static_cast<sal_uInt32>(nEndCol)-nStartCol; ++iLoop)
1252 // add row to table
1253 LwpObjectID *pColID = GetColumnLayoutHead();
1254 LwpColumnLayout * pColumnLayout = dynamic_cast<LwpColumnLayout *>(pColID->obj());
1255 while (pColumnLayout)
1257 if (pColumnLayout->GetColumnID() == (iLoop+nStartCol))
1259 pXFTable->SetColumnStyle(iLoop+1, pColumnLayout->GetStyleName());
1260 break;
1262 pColID = pColumnLayout->GetNext();
1263 pColumnLayout = dynamic_cast<LwpColumnLayout *>(pColID->obj());
1265 if (!pColumnLayout)
1267 pXFTable->SetColumnStyle(iLoop+1, m_DefaultColumnStyleName);
1272 * @short split conflict merged cells
1274 void LwpTableLayout::SplitConflictCells()
1276 LwpTable * pTable = GetTable();
1277 if (!pTable)
1278 return;
1279 sal_uInt16 nCol = pTable->GetColumn();
1280 sal_uInt16 nRow = pTable->GetRow();
1282 sal_uInt16 nEffectRows;
1283 std::map<sal_uInt16,LwpRowLayout*>::iterator iter1;
1284 std::map<sal_uInt16,LwpRowLayout*>::iterator iter2;
1285 LwpRowLayout* pRowLayout;
1286 LwpRowLayout* pEffectRow;
1288 for (sal_uInt16 i=0; i<nRow; )
1290 iter1 = m_RowsMap.find(i);
1291 if (iter1 == m_RowsMap.end())//default rows
1293 i++;
1294 continue;
1296 pRowLayout= iter1->second;
1297 if (pRowLayout->GetMergeCellFlag() == false)
1299 i++;
1300 continue;
1302 else
1304 nEffectRows = i + pRowLayout->GetCurMaxSpannedRows(0,(sal_uInt8)nCol);
1306 for (sal_uInt16 j = i+1; j<nEffectRows; j++)
1308 iter2 = m_RowsMap.find(j);
1309 if (iter2 == m_RowsMap.end())
1310 continue;
1311 pEffectRow = iter2->second;
1312 if (pEffectRow->GetMergeCellFlag() == false)
1313 continue;
1314 else
1315 pEffectRow->SetCellSplit(nEffectRows);
1317 i = nEffectRows;
1319 }//end for
1323 * @short add default row which are missing in the file
1324 * @param pXFTable - pointer to new created table
1325 * @param nStartCol - starting column
1326 * @param nEndCol - end column
1327 * @return pXFTable
1329 void LwpTableLayout::ConvertDefaultRow(XFTable* pXFTable,sal_uInt8 nStartCol,
1330 sal_uInt8 nEndCol,sal_uInt16 nRowID)
1332 // current row doesn't exist in the file
1333 XFRow * pRow = new XFRow();
1334 pRow->SetStyleName(m_DefaultRowStyleName);
1336 for (sal_uInt16 j =0;j < nEndCol-nStartCol; j++)
1338 // if table has default cell layout, use it to ConvertCell
1339 // otherwise use blank cell
1340 XFCell * pCell = NULL;
1341 if (m_pDefaultCellLayout)
1343 pCell = m_pDefaultCellLayout->ConvertCell(
1344 *GetTable()->GetObjectID(),nRowID,j+nStartCol);
1346 else
1348 pCell = new XFCell();
1350 pRow->AddCell(pCell);
1353 pXFTable->AddRow(pRow);
1356 * @short set cell map info
1357 * @param pXFCell - pointer to xfcell
1358 * @param nRow - row id
1359 * @param nCol - column id
1361 void LwpTableLayout::SetCellsMap(sal_uInt16 nRow,sal_uInt8 nCol,XFCell* pXFCell)
1363 std::pair<std::pair<sal_uInt16,sal_uInt8>,XFCell*> cell;
1364 std::pair<sal_uInt16,sal_uInt8> pos;
1365 pos.first = nRow;
1366 pos.second = nCol;
1367 cell.first = pos;
1368 cell.second = pXFCell;
1369 m_CellsMap.insert(cell);
1373 * @short get cell map info
1374 * @param nRow - row id
1375 * @param nCol - column id
1376 * @return pXFCell
1378 XFCell* LwpTableLayout::GetCellsMap(sal_uInt16 nRow,sal_uInt8 nCol)
1380 std::pair<sal_uInt16,sal_uInt8> pos;
1381 pos.first = nRow;
1382 pos.second = nCol;
1383 std::map<std::pair<sal_uInt16,sal_uInt8>,XFCell*>::iterator iter;
1384 iter = m_CellsMap.find(pos);
1385 if (iter == m_CellsMap.end())
1386 return NULL;
1387 return iter->second;
1390 * @descr Get row layout by row id
1391 * @param nRow - row id
1393 LwpRowLayout* LwpTableLayout::GetRowLayout(sal_uInt16 nRow)
1395 LwpObjectID *pRowID = GetChildHead();
1396 LwpRowLayout * pRowLayout = dynamic_cast<LwpRowLayout *>(pRowID->obj());
1397 while (pRowLayout)
1399 if(pRowLayout->GetRowID() == nRow)
1400 return pRowLayout;
1402 pRowID = pRowLayout->GetNext();
1403 pRowLayout = dynamic_cast<LwpRowLayout *>(pRowID->obj());
1405 return NULL;
1408 //add end by
1409 LwpColumnLayout::LwpColumnLayout(LwpObjectHeader &objHdr, LwpSvStream* pStrm)
1410 : LwpVirtualLayout(objHdr, pStrm)
1411 , ccolid(0)
1412 , cwidth(0)
1415 LwpColumnLayout::~LwpColumnLayout()
1417 void LwpColumnLayout::Read()
1419 LwpObjectStream* pStrm = m_pObjStrm;
1421 LwpVirtualLayout::Read();
1423 sal_uInt16 colid;
1425 colid = pStrm->QuickReaduInt16(); // forced to lushort
1426 ccolid = (sal_uInt8)colid; // Phillip
1427 cwidth = pStrm->QuickReadInt32();
1429 pStrm->SkipExtra();
1432 void LwpColumnLayout::RegisterStyle(double dCalculatedWidth)
1434 XFColStyle * pColStyle = new XFColStyle();
1435 pColStyle->SetWidth(static_cast<float>(dCalculatedWidth));
1436 XFStyleManager* pXFStyleManager = LwpGlobalMgr::GetInstance()->GetXFStyleManager();
1437 m_StyleName = pXFStyleManager->AddStyle(pColStyle)->GetStyleName();
1440 LwpTableHeadingLayout::LwpTableHeadingLayout(LwpObjectHeader &objHdr, LwpSvStream* pStrm)
1441 : LwpTableLayout(objHdr, pStrm)
1442 , cStartRow(0)
1443 , cEndRow(0)
1446 LwpTableHeadingLayout::~LwpTableHeadingLayout()
1449 * @short read table heading layout
1450 * @param
1451 * @return
1453 void LwpTableHeadingLayout::Read()
1455 LwpTableLayout::Read();
1457 cStartRow = m_pObjStrm->QuickReaduInt16();
1458 cEndRow = m_pObjStrm->QuickReaduInt16();
1460 m_pObjStrm->SkipExtra();
1464 * @short get start and end row number of table heading
1465 * @param
1466 * @return *pStartRow - starting row number
1467 * @return *pEndRow - end row number
1469 void LwpTableHeadingLayout::GetStartEndRow(sal_uInt16& nStartRow, sal_uInt16& nEndRow)
1471 nStartRow = cStartRow;
1472 nEndRow = cEndRow;
1475 * @short get first row heading layout of table heading
1476 * @param
1477 * @return LwpRowHeadingLayout * - pointer to first row heading layout of table heading
1479 LwpRowHeadingLayout * LwpTableHeadingLayout::GetFirstRowHeadingLayout()
1481 LwpObjectID *pID = GetChildHead();
1482 if(pID && !pID->IsNull())
1484 LwpRowHeadingLayout * pHeadingRow = dynamic_cast<LwpRowHeadingLayout *>(pID->obj());
1485 return pHeadingRow;
1487 return NULL;
1490 LwpSuperParallelColumnLayout::LwpSuperParallelColumnLayout(LwpObjectHeader &objHdr, LwpSvStream* pStrm):LwpSuperTableLayout(objHdr, pStrm)
1493 LwpSuperParallelColumnLayout::~LwpSuperParallelColumnLayout()
1496 void LwpSuperParallelColumnLayout::Read()
1498 LwpSuperTableLayout::Read();
1499 m_pObjStrm->SkipExtra();
1503 LwpSuperGlossaryLayout::LwpSuperGlossaryLayout(LwpObjectHeader &objHdr, LwpSvStream* pStrm):LwpSuperTableLayout(objHdr, pStrm)
1507 LwpSuperGlossaryLayout::~LwpSuperGlossaryLayout()
1511 void LwpSuperGlossaryLayout::Read()
1513 LwpSuperTableLayout::Read();
1514 m_pObjStrm->SkipExtra();
1517 LwpParallelColumnsLayout::LwpParallelColumnsLayout(LwpObjectHeader &objHdr, LwpSvStream* pStrm):LwpTableLayout(objHdr, pStrm)
1521 LwpParallelColumnsLayout::~LwpParallelColumnsLayout()
1525 void LwpParallelColumnsLayout::Read()
1527 LwpTableLayout::Read();
1528 m_pObjStrm->SkipExtra();
1531 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */