LibreOffice Module editeng (master) 1
paralist.cxx
Go to the documentation of this file.
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
21#include "paralist.hxx"
22
23#include <editeng/outliner.hxx>
24#include <editeng/numdef.hxx>
25#include <o3tl/safeint.hxx>
26#include <osl/diagnose.h>
27#include <sal/log.hxx>
28#include <tools/debug.hxx>
29#include <libxml/xmlwriter.h>
30
32: nDepth( -1 )
33, mnNumberingStartValue( -1 )
34, mbParaIsNumberingRestart( false )
35{
36}
37
38bool ParagraphData::operator==(const ParagraphData& rCandidate) const
39{
40 return (nDepth == rCandidate.nDepth
43}
44
45Paragraph::Paragraph( sal_Int16 nDDepth )
46: aBulSize( -1, -1)
47{
48
49 DBG_ASSERT( ( nDDepth >= -1 ) && ( nDDepth < SVX_MAX_NUM ), "Paragraph-CTOR: nDepth invalid!" );
50
51 nDepth = nDDepth;
53 bVisible = true;
54}
55
57: aBulSize( -1, -1)
58, nFlags( ParaFlag::NONE )
59, bVisible( true )
60{
61 nDepth = rData.nDepth;
64}
65
67{
68}
69
70void Paragraph::SetNumberingStartValue( sal_Int16 nNumberingStartValue )
71{
72 mnNumberingStartValue = nNumberingStartValue;
73 if( mnNumberingStartValue != -1 )
75}
76
77void Paragraph::SetParaIsNumberingRestart( bool bParaIsNumberingRestart )
78{
79 mbParaIsNumberingRestart = bParaIsNumberingRestart;
82}
83
85{
86 (void)xmlTextWriterStartElement(pWriter, BAD_CAST("Paragraph"));
87 (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("nDepth"), "%" SAL_PRIdINT32, static_cast<sal_Int32>(nDepth));
88 (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("mnNumberingStartValue"), "%" SAL_PRIdINT32, static_cast<sal_Int32>(mnNumberingStartValue));
89 (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("mbParaIsNumberingRestart"), "%" SAL_PRIdINT32, static_cast<sal_Int32>(mbParaIsNumberingRestart));
90 (void)xmlTextWriterEndElement(pWriter);
91}
92
94{
95 maEntries.clear();
96}
97
98void ParagraphList::Append( std::unique_ptr<Paragraph> pPara)
99{
100 SAL_WARN_IF( maEntries.size() >= EE_PARA_MAX_COUNT, "editeng", "ParagraphList::Append - overflow");
101 maEntries.push_back(std::move(pPara));
102}
103
104void ParagraphList::Insert( std::unique_ptr<Paragraph> pPara, sal_Int32 nAbsPos)
105{
106 SAL_WARN_IF( nAbsPos < 0 || (maEntries.size() < o3tl::make_unsigned(nAbsPos) && nAbsPos != EE_PARA_APPEND),
107 "editeng", "ParagraphList::Insert - bad insert position " << nAbsPos);
108 SAL_WARN_IF( maEntries.size() >= EE_PARA_MAX_COUNT, "editeng", "ParagraphList::Insert - overflow");
109
110 if (nAbsPos < 0 || maEntries.size() <= o3tl::make_unsigned(nAbsPos))
111 Append( std::move(pPara) );
112 else
113 maEntries.insert(maEntries.begin()+nAbsPos, std::move(pPara));
114}
115
116void ParagraphList::Remove( sal_Int32 nPara )
117{
118 if (nPara < 0 || maEntries.size() <= o3tl::make_unsigned(nPara))
119 {
120 SAL_WARN( "editeng", "ParagraphList::Remove - out of bounds " << nPara);
121 return;
122 }
123
124 maEntries.erase(maEntries.begin() + nPara );
125}
126
127void ParagraphList::MoveParagraphs( sal_Int32 nStart, sal_Int32 nDest, sal_Int32 _nCount )
128{
129 OSL_ASSERT(o3tl::make_unsigned(nStart) < maEntries.size() && o3tl::make_unsigned(nDest) < maEntries.size());
130
131 if ( (( nDest < nStart ) || ( nDest >= ( nStart + _nCount ) )) && nStart >= 0 && nDest >= 0 && _nCount >= 0 )
132 {
133 std::vector<std::unique_ptr<Paragraph>> aParas;
134 auto iterBeg = maEntries.begin() + nStart;
135 auto iterEnd = iterBeg + _nCount;
136
137 for (auto it = iterBeg; it != iterEnd; ++it)
138 aParas.push_back(std::move(*it));
139
140 maEntries.erase(iterBeg,iterEnd);
141
142 if ( nDest > nStart )
143 nDest -= _nCount;
144
145 for (auto & i : aParas)
146 {
147 maEntries.insert(maEntries.begin() + nDest, std::move(i));
148 ++nDest;
149 }
150 }
151 else
152 {
153 OSL_FAIL( "MoveParagraphs: Invalid Parameters" );
154 }
155}
156
157bool ParagraphList::HasChildren( Paragraph const * pParagraph ) const
158{
159 sal_Int32 n = GetAbsPos( pParagraph );
160 Paragraph* pNext = GetParagraph( ++n );
161 return pNext && ( pNext->GetDepth() > pParagraph->GetDepth() );
162}
163
164bool ParagraphList::HasHiddenChildren( Paragraph const * pParagraph ) const
165{
166 sal_Int32 n = GetAbsPos( pParagraph );
167 Paragraph* pNext = GetParagraph( ++n );
168 return pNext && ( pNext->GetDepth() > pParagraph->GetDepth() ) && !pNext->IsVisible();
169}
170
171bool ParagraphList::HasVisibleChildren( Paragraph const * pParagraph ) const
172{
173 sal_Int32 n = GetAbsPos( pParagraph );
174 Paragraph* pNext = GetParagraph( ++n );
175 return pNext && ( pNext->GetDepth() > pParagraph->GetDepth() ) && pNext->IsVisible();
176}
177
178sal_Int32 ParagraphList::GetChildCount( Paragraph const * pParent ) const
179{
180 sal_Int32 nChildCount = 0;
181 sal_Int32 n = GetAbsPos( pParent );
182 Paragraph* pPara = GetParagraph( ++n );
183 while ( pPara && ( pPara->GetDepth() > pParent->GetDepth() ) )
184 {
185 nChildCount++;
186 pPara = GetParagraph( ++n );
187 }
188 return nChildCount;
189}
190
191Paragraph* ParagraphList::GetParent( Paragraph const * pParagraph ) const
192{
193 sal_Int32 n = GetAbsPos( pParagraph );
194 Paragraph* pPrev = GetParagraph( --n );
195 while ( pPrev && ( pPrev->GetDepth() >= pParagraph->GetDepth() ) )
196 {
197 pPrev = GetParagraph( --n );
198 }
199
200 return pPrev;
201}
202
203void ParagraphList::Expand( Paragraph const * pParent )
204{
205 sal_Int32 nChildCount = GetChildCount( pParent );
206 sal_Int32 nPos = GetAbsPos( pParent );
207
208 for ( sal_Int32 n = 1; n <= nChildCount; n++ )
209 {
210 Paragraph* pPara = GetParagraph( nPos+n );
211 if ( !( pPara->IsVisible() ) )
212 {
213 pPara->bVisible = true;
215 }
216 }
217}
218
219void ParagraphList::Collapse( Paragraph const * pParent )
220{
221 sal_Int32 nChildCount = GetChildCount( pParent );
222 sal_Int32 nPos = GetAbsPos( pParent );
223
224 for ( sal_Int32 n = 1; n <= nChildCount; n++ )
225 {
226 Paragraph* pPara = GetParagraph( nPos+n );
227 if ( pPara->IsVisible() )
228 {
229 pPara->bVisible = false;
231 }
232 }
233}
234
235sal_Int32 ParagraphList::GetAbsPos( Paragraph const * pParent ) const
236{
237 sal_Int32 pos = 0;
238 for (auto const& entry : maEntries)
239 {
240 if (entry.get() == pParent)
241 return pos;
242 ++pos;
243 }
244
245 return EE_PARA_NOT_FOUND;
246}
247
249{
250 (void)xmlTextWriterStartElement(pWriter, BAD_CAST("ParagraphList"));
251 for (auto const & pParagraph : maEntries)
252 pParagraph->dumpAsXml(pWriter);
253 (void)xmlTextWriterEndElement(pWriter);
254}
255
256/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
friend class Paragraph
bool operator==(const ParagraphData &rCandidate) const
Definition: paralist.cxx:38
sal_Int16 nDepth
sal_Int16 mnNumberingStartValue
bool mbParaIsNumberingRestart
Paragraph * GetParagraph(sal_Int32 nPos) const
Definition: paralist.hxx:51
void Remove(sal_Int32 nPara)
Definition: paralist.cxx:116
Link< Paragraph &, void > aVisibleStateChangedHdl
Definition: paralist.hxx:78
void Insert(std::unique_ptr< Paragraph > pPara, sal_Int32 nAbsPos)
Definition: paralist.cxx:104
std::vector< std::unique_ptr< Paragraph > > maEntries
Definition: paralist.hxx:79
void MoveParagraphs(sal_Int32 nStart, sal_Int32 nDest, sal_Int32 nCount)
Definition: paralist.cxx:127
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: paralist.cxx:248
void Clear()
Definition: paralist.cxx:93
bool HasChildren(Paragraph const *pParagraph) const
Definition: paralist.cxx:157
bool HasVisibleChildren(Paragraph const *pParagraph) const
Definition: paralist.cxx:171
sal_Int32 GetChildCount(Paragraph const *pParagraph) const
Definition: paralist.cxx:178
void Append(std::unique_ptr< Paragraph > pPara)
Definition: paralist.cxx:98
void Expand(Paragraph const *pParent)
Definition: paralist.cxx:203
bool HasHiddenChildren(Paragraph const *pParagraph) const
Definition: paralist.cxx:164
void Collapse(Paragraph const *pParent)
Definition: paralist.cxx:219
sal_Int32 GetAbsPos(Paragraph const *pParent) const
Definition: paralist.cxx:235
Paragraph * GetParent(Paragraph const *pParagraph) const
Definition: paralist.cxx:191
sal_Int16 GetDepth() const
Definition: outliner.hxx:145
void SetNumberingStartValue(sal_Int16 nNumberingStartValue)
Definition: paralist.cxx:70
bool bVisible
Definition: outliner.hxx:133
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: paralist.cxx:84
ParaFlag nFlags
Definition: outliner.hxx:132
~Paragraph()
Definition: paralist.cxx:66
bool IsVisible() const
Definition: outliner.hxx:135
void SetParaIsNumberingRestart(bool bParaIsNumberingRestart)
Definition: paralist.cxx:77
#define DBG_ASSERT(sCon, aError)
#define EE_PARA_NOT_FOUND
Definition: editdata.hxx:48
#define EE_PARA_MAX_COUNT
Definition: editdata.hxx:51
#define EE_PARA_APPEND
Definition: editdata.hxx:49
struct _xmlTextWriter * xmlTextWriterPtr
sal_Int64 n
sal_uInt16 nPos
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
NONE
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
#define SVX_MAX_NUM
Definition: numdef.hxx:23
ParaFlag
Definition: outliner.hxx:97
bool bVisible
size_t pos