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