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 <com/sun/star/text/ChapterFormat.hpp>
21 : #include <doc.hxx>
22 : #include <frame.hxx>
23 : #include <pam.hxx>
24 : #include <ndtxt.hxx>
25 : #include <chpfld.hxx>
26 : #include <expfld.hxx>
27 : #include <unofldmid.h>
28 : #include <numrule.hxx>
29 :
30 : using namespace ::com::sun::star;
31 :
32 : namespace
33 : {
34 :
35 0 : OUString removeControlChars(const OUString& sIn)
36 : {
37 0 : OUStringBuffer aBuf(sIn.replace('\n', ' '));
38 0 : sal_Int32 nLen = aBuf.getLength();
39 0 : for (sal_Int32 i = 0; i < nLen; ++i)
40 : {
41 0 : if (aBuf[i] < ' ')
42 : {
43 0 : sal_Int32 j = i+1;
44 0 : while (j<nLen && aBuf[j]<' ') ++j;
45 0 : aBuf.remove(i, j-i);
46 0 : nLen = aBuf.getLength();
47 : }
48 : }
49 0 : return aBuf.makeStringAndClear();
50 : }
51 :
52 : }
53 :
54 : // SwChapterFieldType
55 :
56 0 : SwChapterFieldType::SwChapterFieldType()
57 0 : : SwFieldType( RES_CHAPTERFLD )
58 : {
59 0 : }
60 :
61 0 : SwFieldType* SwChapterFieldType::Copy() const
62 : {
63 0 : return new SwChapterFieldType();
64 : }
65 :
66 : // chapter field
67 :
68 0 : SwChapterField::SwChapterField(SwChapterFieldType* pTyp, sal_uInt32 nFmt)
69 0 : : SwField(pTyp, nFmt), nLevel( 0 )
70 0 : {}
71 :
72 0 : OUString SwChapterField::Expand() const
73 : {
74 0 : switch( GetFormat() )
75 : {
76 : case CF_TITLE:
77 0 : return sTitle;
78 : case CF_NUMBER:
79 0 : return sPre + sNumber + sPost;
80 : case CF_NUM_TITLE:
81 0 : return sPre + sNumber + sPost + sTitle;
82 : case CF_NUM_NOPREPST_TITLE:
83 0 : return sNumber + sTitle;
84 : }
85 : // CF_NUMBER_NOPREPST
86 0 : return sNumber;
87 : }
88 :
89 0 : SwField* SwChapterField::Copy() const
90 : {
91 : SwChapterField *pTmp =
92 0 : new SwChapterField((SwChapterFieldType*)GetTyp(), GetFormat());
93 0 : pTmp->nLevel = nLevel;
94 0 : pTmp->sTitle = sTitle;
95 0 : pTmp->sNumber = sNumber;
96 0 : pTmp->sPost = sPost;
97 0 : pTmp->sPre = sPre;
98 :
99 0 : return pTmp;
100 : }
101 :
102 : // #i53420#
103 0 : void SwChapterField::ChangeExpansion(const SwFrm* pFrm,
104 : const SwCntntNode* pCntntNode,
105 : sal_Bool bSrchNum )
106 : {
107 : OSL_ENSURE( pFrm, "In which frame am I?" );
108 0 : SwDoc* pDoc = (SwDoc*)pCntntNode->GetDoc();
109 :
110 0 : const SwTxtNode* pTxtNode = dynamic_cast<const SwTxtNode*>(pCntntNode);
111 0 : if ( !pTxtNode || !pFrm->IsInDocBody() )
112 : {
113 0 : SwPosition aDummyPos( pDoc->GetNodes().GetEndOfContent() );
114 0 : pTxtNode = GetBodyTxtNode( *pDoc, aDummyPos, *pFrm );
115 : }
116 :
117 0 : if ( pTxtNode )
118 : {
119 0 : ChangeExpansion( *pTxtNode, bSrchNum );
120 : }
121 0 : }
122 :
123 0 : void SwChapterField::ChangeExpansion(const SwTxtNode &rTxtNd, sal_Bool bSrchNum)
124 : {
125 : //i120759,this function is for both the reference chapter field and normal chapter field
126 : //bSrchNum can distinguish the two types,to the latter type,the outline num rule is must...
127 0 : sNumber = OUString();
128 0 : sTitle = OUString();
129 0 : sPost = OUString();
130 0 : sPre = OUString();
131 : //The reference chapter field of normal num rule will be handled in this code segment
132 0 : if (bSrchNum && !rTxtNd.IsOutline())
133 : {
134 0 : SwNumRule* pRule(rTxtNd.GetNumRule());
135 0 : if (rTxtNd.IsCountedInList() && pRule)
136 : {
137 0 : sNumber = rTxtNd.GetNumString(false);
138 :
139 0 : int nListLevel = rTxtNd.GetActualListLevel();
140 :
141 0 : if (nListLevel < 0)
142 0 : nListLevel = 0;
143 :
144 0 : if (nListLevel >= MAXLEVEL)
145 0 : nListLevel = MAXLEVEL - 1;
146 :
147 0 : const SwNumFmt& rNFmt = pRule->Get(static_cast<unsigned short>(nListLevel));
148 0 : sPost = rNFmt.GetSuffix();
149 0 : sPre = rNFmt.GetPrefix();
150 : }
151 : else
152 : {
153 0 : sNumber = "??";
154 : }
155 0 : sTitle = removeControlChars(rTxtNd.GetExpandTxt());
156 : }
157 : else
158 : {
159 : //End
160 0 : SwDoc* pDoc = (SwDoc*)rTxtNd.GetDoc();
161 0 : const SwTxtNode *pTxtNd = rTxtNd.FindOutlineNodeOfLevel( nLevel );
162 0 : if( pTxtNd )
163 : {
164 0 : if( bSrchNum )
165 : {
166 0 : const SwTxtNode* pONd = pTxtNd;
167 : do {
168 0 : if( pONd && pONd->GetTxtColl() )
169 : {
170 0 : sal_uInt8 nPrevLvl = nLevel;
171 :
172 : OSL_ENSURE( pONd->GetAttrOutlineLevel() >= 0 && pONd->GetAttrOutlineLevel() <= MAXLEVEL,
173 : "<SwChapterField::ChangeExpansion(..)> - outline node with inconsistent outline level. Serious defect -> please inform OD." );
174 0 : nLevel = static_cast<sal_uInt8>(pONd->GetAttrOutlineLevel());
175 :
176 0 : if( nPrevLvl < nLevel )
177 0 : nLevel = nPrevLvl;
178 0 : else if( SVX_NUM_NUMBER_NONE != pDoc->GetOutlineNumRule()
179 0 : ->Get( nLevel ).GetNumberingType() )
180 : {
181 0 : pTxtNd = pONd;
182 0 : break;
183 : }
184 :
185 0 : if( !nLevel-- )
186 0 : break;
187 0 : pONd = pTxtNd->FindOutlineNodeOfLevel( nLevel );
188 : }
189 : else
190 0 : break;
191 0 : } while( true );
192 : }
193 :
194 : // get the number without Pre-/Post-fixstrings
195 :
196 0 : if ( pTxtNd->IsOutline() )
197 : {
198 : // correction of refactoring done by cws swnumtree:
199 : // retrieve numbering string without prefix and suffix strings
200 : // as stated in the above german comment.
201 0 : sNumber = pTxtNd->GetNumString( false );
202 :
203 0 : SwNumRule* pRule( pTxtNd->GetNumRule() );
204 0 : if ( pTxtNd->IsCountedInList() && pRule )
205 : {
206 0 : int nListLevel = pTxtNd->GetActualListLevel();
207 0 : if (nListLevel < 0)
208 0 : nListLevel = 0;
209 0 : if (nListLevel >= MAXLEVEL)
210 0 : nListLevel = MAXLEVEL - 1;
211 :
212 0 : const SwNumFmt& rNFmt = pRule->Get(nListLevel);
213 0 : sPost = rNFmt.GetSuffix();
214 0 : sPre = rNFmt.GetPrefix();
215 : }
216 : }
217 : else
218 : {
219 0 : sNumber = "??";
220 : }
221 :
222 0 : sTitle = removeControlChars(pTxtNd->GetExpandTxt(0, -1, false, false, false, false));
223 :
224 : }
225 : }
226 0 : }
227 :
228 0 : bool SwChapterField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
229 : {
230 0 : switch( nWhichId )
231 : {
232 : case FIELD_PROP_BYTE1:
233 0 : rAny <<= (sal_Int8)nLevel;
234 0 : break;
235 :
236 : case FIELD_PROP_USHORT1:
237 : {
238 : sal_Int16 nRet;
239 0 : switch( GetFormat() )
240 : {
241 0 : case CF_NUMBER: nRet = text::ChapterFormat::NUMBER; break;
242 0 : case CF_TITLE: nRet = text::ChapterFormat::NAME; break;
243 : case CF_NUMBER_NOPREPST:
244 0 : nRet = text::ChapterFormat::DIGIT;
245 0 : break;
246 : case CF_NUM_NOPREPST_TITLE:
247 0 : nRet = text::ChapterFormat::NO_PREFIX_SUFFIX;
248 0 : break;
249 : case CF_NUM_TITLE:
250 0 : default: nRet = text::ChapterFormat::NAME_NUMBER;
251 : }
252 0 : rAny <<= nRet;
253 : }
254 0 : break;
255 :
256 : default:
257 : OSL_FAIL("illegal property");
258 : }
259 0 : return true;
260 : }
261 :
262 0 : bool SwChapterField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
263 : {
264 0 : sal_Bool bRet = sal_True;
265 0 : switch( nWhichId )
266 : {
267 : case FIELD_PROP_BYTE1:
268 : {
269 0 : sal_Int8 nTmp = 0;
270 0 : rAny >>= nTmp;
271 0 : if(nTmp >= 0 && nTmp < MAXLEVEL)
272 0 : nLevel = nTmp;
273 : else
274 0 : bRet = false;
275 0 : break;
276 : }
277 :
278 : case FIELD_PROP_USHORT1:
279 : {
280 0 : sal_Int16 nVal = 0;
281 0 : rAny >>= nVal;
282 0 : switch( nVal )
283 : {
284 0 : case text::ChapterFormat::NAME: SetFormat(CF_TITLE); break;
285 0 : case text::ChapterFormat::NUMBER: SetFormat(CF_NUMBER); break;
286 : case text::ChapterFormat::NO_PREFIX_SUFFIX:
287 0 : SetFormat(CF_NUM_NOPREPST_TITLE);
288 0 : break;
289 : case text::ChapterFormat::DIGIT:
290 0 : SetFormat(CF_NUMBER_NOPREPST);
291 0 : break;
292 :
293 0 : default: SetFormat(CF_NUM_TITLE);
294 : }
295 : }
296 0 : break;
297 :
298 : default:
299 : OSL_FAIL("illegal property");
300 0 : bRet = false;
301 : }
302 0 : return bRet;
303 : }
304 :
305 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|