Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / lotuswordpro / source / filter / lwptoc.cxx
blob27ab9e326bc0a20cb42073e152ddbad1f1dd19d7
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 - TOC related object
61 #include "lwptoc.hxx"
62 #include "lwpfoundry.hxx"
63 #include "lwpdoc.hxx"
64 #include "lwpframelayout.hxx"
65 #include "lwpglobalmgr.hxx"
67 #include "xfilter/xffont.hxx"
68 #include "xfilter/xftextstyle.hxx"
69 #include "xfilter/xfstylemanager.hxx"
70 #include "xfilter/xfparagraph.hxx"
71 #include "xfilter/xfparastyle.hxx"
72 #include "xfilter/xfindex.hxx"
73 #include "xfilter/xffloatframe.hxx"
74 #include "xfilter/xfframestyle.hxx"
75 #include "xfilter/xfframe.hxx"
76 #include "xfilter/xftable.hxx"
78 LwpTocSuperLayout::LwpTocSuperLayout(LwpObjectHeader &objHdr, LwpSvStream* pStrm)
79 : LwpSuperTableLayout(objHdr, pStrm)
80 , m_nFrom(0)
81 , m_pCont(nullptr)
85 LwpTocSuperLayout::~LwpTocSuperLayout()
89 /**
90 * @short Read TOCSUPERTABLELAYOUT object
91 * @return none
93 void LwpTocSuperLayout::Read()
95 LwpSuperTableLayout::Read();
96 m_TextMarker.Read(m_pObjStrm);
97 m_ParentName.Read(m_pObjStrm);
98 m_DivisionName.Read(m_pObjStrm);
99 m_SectionName.Read(m_pObjStrm);
100 m_nFrom = m_pObjStrm->QuickReaduInt16();
102 m_SearchItems.Read(m_pObjStrm);
104 sal_uInt16 count = m_pObjStrm->QuickReaduInt16();
105 if (count > MAX_LEVELS)
106 throw std::range_error("corrupt LwpTocSuperLayout");
107 for (sal_uInt16 i = 0; i < count; ++i)
108 m_DestName[i].Read(m_pObjStrm);
110 count = m_pObjStrm->QuickReaduInt16();
111 if (count > MAX_LEVELS)
112 throw std::range_error("corrupt LwpTocSuperLayout");
113 for (sal_uInt16 i = 0; i < count; ++i)
114 m_DestPGName[i].Read(m_pObjStrm);
116 count = m_pObjStrm->QuickReaduInt16();
117 if (count > MAX_LEVELS)
118 throw std::range_error("corrupt LwpTocSuperLayout");
119 for (sal_uInt16 i = 0; i < count; ++i)
120 m_nFlags[i] = m_pObjStrm->QuickReaduInt32();
122 m_pObjStrm->SkipExtra();
125 * @short Register style of TOC
126 * @return none
128 void LwpTocSuperLayout::RegisterStyle()
130 LwpSuperTableLayout::RegisterStyle();
132 // Get font info of default text style and set into tab style
133 const LwpObjectID *pDefaultTextStyle = m_pFoundry ? m_pFoundry->GetDefaultTextStyle() : nullptr;
134 XFParaStyle* pBaseStyle = pDefaultTextStyle ? dynamic_cast<XFParaStyle*>(m_pFoundry->GetStyleManager()->GetStyle(*pDefaultTextStyle)) : nullptr;
135 XFTextStyle*pTextStyle = new XFTextStyle;
136 if (pBaseStyle)
137 pTextStyle->SetFont(pBaseStyle->GetFont()); // who delete this font?????
138 XFStyleManager* pXFStyleManager = LwpGlobalMgr::GetInstance()->GetXFStyleManager();
139 m_TabStyleName = pXFStyleManager->AddStyle(pTextStyle).m_pStyle->GetStyleName();
143 * @short Convert TOC
144 * @param pCont - container
145 * @return none
147 void LwpTocSuperLayout::XFConvert(XFContentContainer* pCont)
149 XFIndex* pToc = new XFIndex();
151 pToc->SetProtected(false);
152 pToc->SetIndexType(enumXFIndexTOC);
154 // add TOC template
155 for (sal_uInt16 i = 1; i<= MAX_LEVELS; i++)
157 LwpTocLevelData * pLevel = GetSearchLevelPtr(i);
158 XFIndexTemplate * pTemplate = new XFIndexTemplate();
160 if(!pLevel)
162 // add an blank template so that SODC won't add default style to this level
163 pToc->AddTemplate(OUString::number(i), OUString(), pTemplate);
164 continue;
167 bool bInserted = false;
170 // One level has 1 template
171 if (!bInserted)
173 pTemplate->SetLevel(OUString::number(i));
174 if(pLevel->GetUseLeadingText())
176 pTemplate->AddEntry(enumXFIndexTemplateChapter, pLevel->GetSearchStyle());
178 if(pLevel->GetUseText())
180 pTemplate->AddEntry(enumXFIndexTemplateText, pLevel->GetSearchStyle());
182 if(GetUsePageNumber(i))
184 sal_uInt16 nLeaderType = GetSeparatorType(i);
185 if (GetRightAlignPageNumber(i))
187 char cSep = ' ';
188 switch(nLeaderType)
190 default: // go through
191 case NONE: // no leaders
192 cSep = ' ';
193 break;
194 case LEADERDOTS:
195 cSep = '.';
196 break;
197 case LEADERDASHES:
198 cSep = '-';
199 break;
200 case LEADERUNDERLINE:
201 cSep = '_';
202 break;
205 pTemplate->AddTabEntry(enumXFTabRight, 0, cSep, 'd', m_TabStyleName);
207 else
209 OUString sSep;
210 switch(nLeaderType)
212 default: // go through
213 case NONE: // no leaders
214 sSep = " ";
215 break;
216 case SEPARATORCOMMA:
217 sSep = ", ";
218 break;
219 case SEPARATORDOTS:
220 sSep = "...";
221 break;
223 pTemplate->AddTextEntry(sSep, m_TabStyleName);
225 //"TOC Page Number Text Style" style always exists in Word Pro file
226 pTemplate->AddEntry(enumXFIndexTemplatePage, "TOC Page Number Text Style");
229 pToc->AddTemplate(OUString::number((sal_Int32)i), m_pFoundry->FindActuralStyleName(pLevel->GetSearchStyle()), pTemplate);
230 bInserted = true;
233 // 1 style in WordPro may be mapped to several styles in SODC
234 LwpDocument * pDocument = m_pFoundry->GetDocument()->GetRootDocument();
235 AddSourceStyle(pToc, pLevel, pDocument->GetFoundry());
237 // one level may have several corresponding Styles
238 pLevel = GetNextSearchLevelPtr(i, pLevel); // find next LwpTocLevelData which is same index
239 }while (pLevel != nullptr);
242 m_pCont = pCont;
243 // add TOC content
244 LwpSuperTableLayout::XFConvert(pToc);
246 rtl::Reference<LwpVirtualLayout> xContainer(GetContainerLayout());
247 if (!xContainer.is())
248 return;
250 // if current TOC is located in a cell, we must add a frame between upper level container and TOC
251 if (!xContainer->IsCell())
253 pCont->Add(pToc);
258 * @short convert frame which anchor to page
259 * @param pCont -
260 * @return
262 void LwpTocSuperLayout::XFConvertFrame(XFContentContainer* pCont, sal_Int32 nStart, sal_Int32 nEnd, bool bAll)
264 if(m_pFrame)
266 XFFrame* pXFFrame = nullptr;
267 if(nEnd < nStart)
269 pXFFrame = new XFFrame();
271 else
273 pXFFrame = new XFFloatFrame(nStart, nEnd, bAll);
276 m_pFrame->Parse(pXFFrame, static_cast<sal_uInt16>(nStart));
278 //parse table, and add table to frame or TOC
279 LwpTableLayout * pTableLayout = GetTableLayout();
280 if (pTableLayout)
282 XFContentContainer * pTableContainer = pXFFrame;
283 // if *this is a TOCSuperTableLayout and it's located in a cell
284 // add the frame to upper level and add TOCSuperTableLayout into the frame
285 rtl::Reference<LwpVirtualLayout> xContainer(GetContainerLayout());
286 if (!xContainer.is())
287 return;
288 if (xContainer->IsCell())
290 pTableContainer = pCont; // TOC contain table directly
291 pXFFrame->Add(pCont);
292 m_pCont->Add(pXFFrame);
294 else
296 //add frame to the container
297 pCont->Add(pXFFrame);
299 pTableLayout->XFConvert(pTableContainer);
306 * @short Add source style into TOC
307 * @param pToc - TOC pointer
308 * @param pLevel - TOC level data
309 * @param pFoundry - foundry pointer
310 * @return sal_Bool
312 void LwpTocSuperLayout::AddSourceStyle(XFIndex* pToc, LwpTocLevelData * pLevel, LwpFoundry * pFoundry)
314 if (!pLevel)
316 return;
319 OUString sLwpStyleName = pLevel->GetSearchStyle();
321 if (pFoundry)
323 LwpDocument * pDoc = pFoundry->GetDocument();
324 if (pDoc && pDoc->IsChildDoc())
326 OUString sSodcStyleName = pFoundry->FindActuralStyleName(sLwpStyleName);
327 pToc->AddTocSource(pLevel->GetLevel(), sSodcStyleName);
329 else
331 pDoc = pDoc->GetFirstDivision();
332 while (pDoc)
334 AddSourceStyle(pToc, pLevel, pDoc->GetFoundry() );
335 pDoc = pDoc->GetNextDivision();
342 * @short Get whether page number is right alignment
343 * @param index - TOC level
344 * @return sal_Bool
346 bool LwpTocSuperLayout::GetRightAlignPageNumber(sal_uInt16 index)
348 if (index < MAX_LEVELS)
349 return (m_nFlags[index] & TS_RIGHTALIGN) != 0;
350 return false;
353 * @short Get whether page number is used in TOC entries
354 * @param index - TOC level
355 * @return sal_Bool
357 bool LwpTocSuperLayout::GetUsePageNumber(sal_uInt16 index)
359 if (index < MAX_LEVELS)
360 return (m_nFlags[index] & TS_PAGENUMBER) != 0;
361 return false;
364 * @short Get what is used for separater
365 * @param index - TOC level
366 * @return sal_uInt16 - separator type
368 sal_uInt16 LwpTocSuperLayout::GetSeparatorType(sal_uInt16 index)
370 if (index >= MAX_LEVELS)
371 return NONE;
373 sal_uInt16 Flag = (sal_uInt16)m_nFlags[index];
375 if (Flag & TS_LEADERDOTS)
376 return LEADERDOTS;
377 else if (Flag & TS_LEADERDASHES)
378 return LEADERDASHES;
379 else if (Flag & TS_LEADERUNDERLINE)
380 return LEADERUNDERLINE;
381 else if (Flag & TS_SEPARATORCOMMA)
382 return SEPARATORCOMMA;
383 else if (Flag & TS_SEPARATORDOTS)
384 return SEPARATORDOTS;
385 else
386 return NONE;
390 * @short Get TOCLEVELDATA obj
391 * @param index - TOC level
392 * @return LwpTocLevelData * - pointer to TOCLEVELDATA obj
394 LwpTocLevelData * LwpTocSuperLayout::GetSearchLevelPtr(sal_uInt16 index)
396 LwpObjectID& rID = m_SearchItems.GetHead();
397 LwpTocLevelData * pObj = dynamic_cast<LwpTocLevelData *>(rID.obj().get());
399 while(pObj)
401 if(pObj->GetLevel()== index)
403 return pObj;
406 rID = pObj->GetNext();
407 pObj = dynamic_cast<LwpTocLevelData *>(rID.obj().get());
410 return nullptr;
413 * @short Get next TOCLEVELDATA obj from current position
414 * @param index - TOC level
415 * @param pCurData - current LwpTocLevelData
416 * @return LwpTocLevelData * - pointer to TOCLEVELDATA obj
418 LwpTocLevelData * LwpTocSuperLayout::GetNextSearchLevelPtr(sal_uInt16 index, LwpTocLevelData * pCurData)
420 LwpObjectID& rID = pCurData->GetNext();
421 LwpTocLevelData * pObj = dynamic_cast<LwpTocLevelData *>(rID.obj().get());
423 while(pObj)
425 if(pObj->GetLevel()== index)
427 return pObj;
430 rID = pObj->GetNext();
431 pObj = dynamic_cast<LwpTocLevelData *>(rID.obj().get());
434 return nullptr;
437 LwpTocLevelData::LwpTocLevelData(LwpObjectHeader &objHdr, LwpSvStream* pStrm):LwpDLVList(objHdr, pStrm)
439 m_nFlags = 0;
440 m_nLevel = 0;
442 LwpTocLevelData::~LwpTocLevelData()
446 * @short Register style
447 * @param
448 * @return
450 void LwpTocLevelData::RegisterStyle()
454 * @short Convert
455 * @param pCont - container
456 * @return none
458 void LwpTocLevelData::XFConvert(XFContentContainer* /*pCont*/)
462 * @short Read TOCLEVELDATA obj
463 * @param
464 * @return
466 void LwpTocLevelData::Read()
468 LwpDLVList::Read();
469 m_nFlags = m_pObjStrm->QuickReaduInt16();
470 m_nLevel = m_pObjStrm->QuickReaduInt16();
471 m_SearchName.Read(m_pObjStrm);
473 m_pObjStrm->SkipExtra();
476 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */