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 <editeng/svxenum.hxx>
21 : #include <numrule.hxx>
22 : #include <SwNodeNum.hxx>
23 : #include <ndtxt.hxx>
24 : #include <pam.hxx>
25 : #include <stdio.h>
26 : // #i83479#
27 : #include <IDocumentListItems.hxx>
28 : #include <doc.hxx>
29 :
30 3541 : SwNodeNum::SwNodeNum( SwTxtNode* pTxtNode )
31 : : SwNumberTreeNode(),
32 : mpTxtNode( pTxtNode ),
33 3541 : mpNumRule( 0 )
34 : {
35 3541 : }
36 :
37 86414 : SwNodeNum::SwNodeNum( SwNumRule* pNumRule )
38 : : SwNumberTreeNode(),
39 : mpTxtNode( 0 ),
40 86414 : mpNumRule( pNumRule )
41 : {
42 86414 : }
43 :
44 179841 : SwNodeNum::~SwNodeNum()
45 : {
46 179841 : }
47 :
48 277845 : SwTxtNode * SwNodeNum::GetTxtNode() const
49 : {
50 277845 : return mpTxtNode;
51 : }
52 :
53 148653 : SwNumRule * SwNodeNum::GetNumRule() const
54 : {
55 148653 : return mpNumRule;
56 : }
57 :
58 0 : void SwNodeNum::ChangeNumRule( SwNumRule& rNumRule )
59 : {
60 : OSL_ENSURE( GetNumRule() && GetTxtNode(),
61 : "<SwNodeNum::ChangeNumRule(..)> - missing list style and/or text node. Serious defect -> please informm OD." );
62 0 : if ( GetNumRule() && GetTxtNode() )
63 : {
64 0 : GetNumRule()->RemoveTxtNode( *(GetTxtNode()) );
65 : }
66 :
67 0 : mpNumRule = &rNumRule;
68 :
69 0 : if ( GetNumRule() && GetTxtNode() )
70 : {
71 0 : GetNumRule()->AddTxtNode( *(GetTxtNode()) );
72 : }
73 0 : }
74 :
75 3514 : SwPosition SwNodeNum::GetPosition() const
76 : {
77 : OSL_ENSURE( GetTxtNode(),
78 : "<SwNodeNum::GetPosition()> - no text node set at <SwNodeNum> instance" );
79 3514 : return SwPosition(*mpTxtNode);
80 : }
81 :
82 1309 : SwNumberTreeNode * SwNodeNum::Create() const
83 : {
84 1309 : SwNodeNum * pResult = new SwNodeNum( GetNumRule() );
85 :
86 1309 : return pResult;
87 : }
88 :
89 4431 : void SwNodeNum::PreAdd()
90 : {
91 : OSL_ENSURE( GetTxtNode(),
92 : "<SwNodeNum::PreAdd()> - no text node set at <SwNodeNum> instance" );
93 4431 : if ( !GetNumRule() && GetTxtNode() )
94 : {
95 4431 : mpNumRule = GetTxtNode()->GetNumRule();
96 : }
97 : OSL_ENSURE( GetNumRule(),
98 : "<SwNodeNum::PreAdd()> - no list style set at <SwNodeNum> instance" );
99 4431 : if ( GetNumRule() && GetTxtNode() )
100 : {
101 4431 : GetNumRule()->AddTxtNode( *(GetTxtNode()) );
102 : }
103 :
104 : {
105 8862 : if ( GetTxtNode() &&
106 4431 : GetTxtNode()->GetNodes().IsDocNodes() )
107 : {
108 4431 : GetTxtNode()->getIDocumentListItems().addListItem( *this );
109 : }
110 : }
111 4431 : }
112 :
113 4431 : void SwNodeNum::PostRemove()
114 : {
115 : OSL_ENSURE( GetTxtNode(),
116 : "<SwNodeNum::PostRemove()> - no text node set at <SwNodeNum> instance" );
117 : OSL_ENSURE( GetNumRule(),
118 : "<SwNodeNum::PostRemove()> - no list style set at <SwNodeNum> instance" );
119 :
120 4431 : if ( GetTxtNode() )
121 : {
122 4431 : GetTxtNode()->getIDocumentListItems().removeListItem( *this );
123 : }
124 :
125 4431 : if ( GetNumRule() )
126 : {
127 4431 : if ( GetTxtNode() )
128 : {
129 4431 : GetNumRule()->RemoveTxtNode( *(GetTxtNode()) );
130 : }
131 4431 : mpNumRule = 0;
132 : }
133 4431 : }
134 :
135 64068 : bool SwNodeNum::IsNotifiable() const
136 : {
137 64068 : bool aResult = true;
138 :
139 64068 : if ( GetTxtNode() )
140 57390 : aResult = GetTxtNode()->IsNotifiable();
141 :
142 64068 : return aResult;
143 : }
144 :
145 4431 : bool SwNodeNum::IsNotificationEnabled() const
146 : {
147 4431 : bool aResult = true;
148 :
149 4431 : if ( GetTxtNode() )
150 4431 : aResult = GetTxtNode()->IsNotificationEnabled();
151 :
152 4431 : return aResult;
153 : }
154 :
155 15667 : bool SwNodeNum::IsContinuous() const
156 : {
157 15667 : bool aResult = false;
158 :
159 : // #i64311#
160 15667 : if ( GetNumRule() )
161 : {
162 15667 : aResult = mpNumRule->IsContinusNum();
163 : }
164 0 : else if ( GetParent() )
165 : {
166 0 : aResult = GetParent()->IsContinuous();
167 : }
168 : else
169 : {
170 : OSL_FAIL( "<SwNodeNum::IsContinuous()> - OD debug" );
171 : }
172 :
173 15667 : return aResult;
174 : }
175 :
176 3937 : bool SwNodeNum::IsCounted() const
177 : {
178 3937 : bool aResult = false;
179 :
180 3937 : if ( GetTxtNode() )
181 : {
182 : // #i59559#
183 : // <SwTxtNode::IsCounted()> determines, if a text node is counted for numbering
184 3446 : aResult = GetTxtNode()->IsCountedInList();
185 : }
186 : else
187 491 : aResult = SwNumberTreeNode::IsCounted();
188 :
189 3937 : return aResult;
190 : }
191 :
192 : // #i64010#
193 1024 : bool SwNodeNum::HasCountedChildren() const
194 : {
195 1024 : bool bResult = false;
196 :
197 1024 : tSwNumberTreeChildren::const_iterator aIt;
198 :
199 1725 : for (aIt = mChildren.begin(); aIt != mChildren.end(); ++aIt)
200 : {
201 767 : SwNodeNum* pChild( dynamic_cast<SwNodeNum*>(*aIt) );
202 : OSL_ENSURE( pChild,
203 : "<SwNodeNum::HasCountedChildren()> - unexcepted type of child -> please inform OD" );
204 1600 : if ( pChild &&
205 1468 : ( pChild->IsCountedForNumbering() ||
206 701 : pChild->HasCountedChildren() ) )
207 : {
208 66 : bResult = true;
209 :
210 66 : break;
211 : }
212 : }
213 :
214 1024 : return bResult;
215 : }
216 : // #i64010#
217 767 : bool SwNodeNum::IsCountedForNumbering() const
218 : {
219 1418 : return IsCounted() &&
220 1158 : ( IsPhantom() || // phantoms
221 1146 : !GetTxtNode() || // root node
222 1095 : GetTxtNode()->HasNumber() || // text node
223 1289 : GetTxtNode()->HasBullet() ); // text node
224 : }
225 :
226 931 : void SwNodeNum::NotifyNode()
227 : {
228 931 : ValidateMe();
229 :
230 931 : if (mpTxtNode)
231 : {
232 928 : mpTxtNode->NumRuleChgd();
233 : }
234 931 : }
235 :
236 166858 : bool SwNodeNum::LessThan(const SwNumberTreeNode & rNode) const
237 : {
238 166858 : bool bResult = false;
239 166858 : const SwNodeNum & rTmpNode = static_cast<const SwNodeNum &>(rNode);
240 :
241 166858 : if (mpTxtNode == NULL && rTmpNode.mpTxtNode != NULL)
242 2504 : bResult = true;
243 164354 : else if (mpTxtNode != NULL && rTmpNode.mpTxtNode != NULL)
244 : {
245 : // #i83479# - refactoring
246 : // simplify comparison by comparing the indexes of the text nodes
247 152115 : bResult = ( mpTxtNode->GetIndex() < rTmpNode.mpTxtNode->GetIndex() ) ? true : false;
248 : }
249 :
250 166858 : return bResult;
251 : }
252 :
253 1372 : bool SwNodeNum::IsRestart() const
254 : {
255 1372 : bool bIsRestart = false;
256 :
257 1372 : if ( GetTxtNode() )
258 : {
259 1316 : bIsRestart = GetTxtNode()->IsListRestart();
260 : }
261 :
262 1372 : return bIsRestart;
263 : }
264 :
265 309 : bool SwNodeNum::IsCountPhantoms() const
266 : {
267 309 : bool bResult = true;
268 :
269 : // #i64311#
270 : // phantoms aren't counted in consecutive numbering rules
271 309 : if ( mpNumRule )
272 618 : bResult = !mpNumRule->IsContinusNum() &&
273 618 : mpNumRule->IsCountPhantoms();
274 : else
275 : {
276 : OSL_FAIL( "<SwNodeNum::IsCountPhantoms(): missing numbering rule - please inform OD" );
277 : }
278 :
279 309 : return bResult;
280 : }
281 :
282 338 : SwNumberTree::tSwNumTreeNumber SwNodeNum::GetStartValue() const
283 : {
284 338 : SwNumberTree::tSwNumTreeNumber aResult = 1;
285 :
286 338 : if ( IsRestart() && GetTxtNode() )
287 : {
288 10 : aResult = GetTxtNode()->GetActualListStartValue();
289 : }
290 : else
291 : {
292 328 : SwNumRule * pRule = GetNumRule();
293 :
294 328 : if (pRule)
295 : {
296 328 : int nLevel = GetParent() ? GetLevelInListTree() : 0;
297 :
298 328 : if (nLevel >= 0 && nLevel < MAXLEVEL)
299 : {
300 328 : const SwNumFmt * pFmt = pRule->GetNumFmt( static_cast<sal_uInt16>(nLevel));
301 :
302 328 : if (pFmt)
303 252 : aResult = pFmt->GetStart();
304 : }
305 : }
306 : }
307 :
308 338 : return aResult;
309 : }
310 :
311 85085 : void SwNodeNum::HandleNumberTreeRootNodeDelete( SwNodeNum& rNodeNum )
312 : {
313 85085 : SwNodeNum* pRootNode = rNodeNum.GetParent()
314 0 : ? dynamic_cast<SwNodeNum*>(rNodeNum.GetRoot())
315 85085 : : &rNodeNum;
316 85085 : if ( !pRootNode )
317 : {
318 : // no root node -> nothing do.
319 85085 : return;
320 : }
321 :
322 : // unregister all number tree node entries, which correspond to a text node,
323 : // about the deletion of the number tree root node.
324 85085 : _UnregisterMeAndChildrenDueToRootDelete( *pRootNode );
325 : }
326 :
327 85085 : void SwNodeNum::_UnregisterMeAndChildrenDueToRootDelete( SwNodeNum& rNodeNum )
328 : {
329 85085 : const bool bIsPhantom( rNodeNum.IsPhantom() );
330 85085 : tSwNumberTreeChildren::size_type nAllowedChildCount( 0 );
331 85085 : bool bDone( false );
332 255255 : while ( !bDone &&
333 85085 : rNodeNum.GetChildCount() > nAllowedChildCount )
334 : {
335 0 : SwNodeNum* pChildNode( dynamic_cast<SwNodeNum*>((*rNodeNum.mChildren.begin())) );
336 0 : if ( !pChildNode )
337 : {
338 : OSL_FAIL( "<SwNodeNum::_UnregisterMeAndChildrenDueToRootDelete(..)> - unknown number tree node child" );
339 0 : ++nAllowedChildCount;
340 0 : continue;
341 : }
342 :
343 : // Unregistering the last child of a phantom will destroy the phantom.
344 : // Thus <rNodeNum> will be destroyed and access on <rNodeNum> has to
345 : // be suppressed.
346 0 : if ( bIsPhantom && rNodeNum.GetChildCount() == 1 )
347 : {
348 0 : bDone = true;
349 : }
350 :
351 0 : _UnregisterMeAndChildrenDueToRootDelete( *pChildNode );
352 : }
353 :
354 85085 : if ( !bIsPhantom )
355 : {
356 85085 : SwTxtNode* pTxtNode( rNodeNum.GetTxtNode() );
357 85085 : if ( pTxtNode )
358 : {
359 0 : pTxtNode->RemoveFromList();
360 : // --> clear all list attributes and the list style
361 0 : std::set<sal_uInt16> aResetAttrsArray;
362 0 : aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_LIST_ID );
363 0 : aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_LIST_LEVEL );
364 0 : aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_LIST_ISRESTART );
365 0 : aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_LIST_RESTARTVALUE );
366 0 : aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_LIST_ISCOUNTED );
367 0 : aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_NUMRULE );
368 0 : SwPaM aPam( *pTxtNode );
369 : pTxtNode->GetDoc()->ResetAttrs( aPam, false,
370 : aResetAttrsArray,
371 0 : false );
372 : }
373 : }
374 85085 : }
375 :
376 : // #i81002#
377 27 : const SwNodeNum* SwNodeNum::GetPrecedingNodeNumOf( const SwTxtNode& rTxtNode ) const
378 : {
379 27 : const SwNodeNum* pPrecedingNodeNum( 0 );
380 :
381 : // #i83479#
382 27 : SwNodeNum aNodeNumForTxtNode( const_cast<SwTxtNode*>(&rTxtNode) );
383 :
384 : pPrecedingNodeNum = dynamic_cast<const SwNodeNum*>(
385 27 : GetRoot()
386 27 : ? GetRoot()->GetPrecedingNodeOf( aNodeNumForTxtNode )
387 54 : : GetPrecedingNodeOf( aNodeNumForTxtNode ) );
388 :
389 27 : return pPrecedingNodeNum;
390 : }
391 :
392 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|