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> // SwChapterFieldType::ChangeExpansion()
23 : #include <pam.hxx> // fuer GetBodyTxtNode
24 : #include <ndtxt.hxx>
25 : #include <chpfld.hxx>
26 : #include <expfld.hxx> // fuer GetBodyTxtNode
27 : #include <unofldmid.h>
28 : #include <numrule.hxx>
29 :
30 : using namespace ::com::sun::star;
31 :
32 : /*--------------------------------------------------------------------
33 : Beschreibung: SwChapterFieldType
34 : --------------------------------------------------------------------*/
35 :
36 :
37 276 : SwChapterFieldType::SwChapterFieldType()
38 276 : : SwFieldType( RES_CHAPTERFLD )
39 : {
40 276 : }
41 :
42 :
43 0 : SwFieldType* SwChapterFieldType::Copy() const
44 : {
45 0 : return new SwChapterFieldType();
46 : }
47 :
48 :
49 : /*--------------------------------------------------------------------
50 : Beschreibung: Kapittelfeld
51 : --------------------------------------------------------------------*/
52 :
53 :
54 0 : SwChapterField::SwChapterField(SwChapterFieldType* pTyp, sal_uInt32 nFmt)
55 0 : : SwField(pTyp, nFmt), nLevel( 0 )
56 0 : {}
57 :
58 :
59 0 : String SwChapterField::Expand() const
60 : {
61 0 : String sStr( sNumber );
62 0 : switch( GetFormat() )
63 : {
64 0 : case CF_TITLE: sStr = sTitle; break;
65 :
66 : case CF_NUMBER:
67 0 : case CF_NUM_TITLE: sStr.Insert( sPre, 0 );
68 0 : sStr += sPost;
69 0 : if( CF_NUM_TITLE == GetFormat() )
70 0 : sStr += sTitle;
71 0 : break;
72 :
73 0 : case CF_NUM_NOPREPST_TITLE: sStr += sTitle; break;
74 : }
75 0 : return sStr;
76 : }
77 :
78 :
79 0 : SwField* SwChapterField::Copy() const
80 : {
81 : SwChapterField *pTmp =
82 0 : new SwChapterField((SwChapterFieldType*)GetTyp(), GetFormat());
83 0 : pTmp->nLevel = nLevel;
84 0 : pTmp->sTitle = sTitle;
85 0 : pTmp->sNumber = sNumber;
86 0 : pTmp->sPost = sPost;
87 0 : pTmp->sPre = sPre;
88 :
89 0 : return pTmp;
90 : }
91 :
92 : // #i53420#
93 0 : void SwChapterField::ChangeExpansion(const SwFrm* pFrm,
94 : const SwCntntNode* pCntntNode,
95 : sal_Bool bSrchNum )
96 : {
97 : OSL_ENSURE( pFrm, "in welchem Frame stehe ich denn?" );
98 0 : SwDoc* pDoc = (SwDoc*)pCntntNode->GetDoc();
99 :
100 0 : const SwTxtNode* pTxtNode = dynamic_cast<const SwTxtNode*>(pCntntNode);
101 0 : if ( !pTxtNode || !pFrm->IsInDocBody() )
102 : {
103 0 : SwPosition aDummyPos( pDoc->GetNodes().GetEndOfContent() );
104 0 : pTxtNode = GetBodyTxtNode( *pDoc, aDummyPos, *pFrm );
105 : }
106 :
107 0 : if ( pTxtNode )
108 : {
109 0 : ChangeExpansion( *pTxtNode, bSrchNum );
110 : }
111 0 : }
112 :
113 0 : void SwChapterField::ChangeExpansion(const SwTxtNode &rTxtNd, sal_Bool bSrchNum)
114 : {
115 0 : SwDoc* pDoc = (SwDoc*)rTxtNd.GetDoc();
116 0 : const SwTxtNode *pTxtNd = rTxtNd.FindOutlineNodeOfLevel( nLevel );
117 0 : if( pTxtNd )
118 : {
119 0 : if( bSrchNum )
120 : {
121 0 : const SwTxtNode* pONd = pTxtNd;
122 0 : do {
123 0 : if( pONd && pONd->GetTxtColl() )
124 : {
125 0 : sal_uInt8 nPrevLvl = nLevel;
126 :
127 : OSL_ENSURE( pONd->GetAttrOutlineLevel() >= 0 && pONd->GetAttrOutlineLevel() <= MAXLEVEL,
128 : "<SwChapterField::ChangeExpansion(..)> - outline node with inconsistent outline level. Serious defect -> please inform OD." );
129 0 : nLevel = static_cast<sal_uInt8>(pONd->GetAttrOutlineLevel());
130 :
131 0 : if( nPrevLvl < nLevel )
132 0 : nLevel = nPrevLvl;
133 0 : else if( SVX_NUM_NUMBER_NONE != pDoc->GetOutlineNumRule()
134 0 : ->Get( nLevel ).GetNumberingType() )
135 : {
136 0 : pTxtNd = pONd;
137 0 : break;
138 : }
139 :
140 0 : if( !nLevel-- )
141 0 : break;
142 0 : pONd = pTxtNd->FindOutlineNodeOfLevel( nLevel );
143 : }
144 : else
145 0 : break;
146 : } while( sal_True );
147 : }
148 :
149 : // nur die Nummer besorgen, ohne Pre-/Post-fixstrings
150 :
151 0 : if ( pTxtNd->IsOutline() )
152 : {
153 : // correction of refactoring done by cws swnumtree:
154 : // retrieve numbering string without prefix and suffix strings
155 : // as stated in the above german comment.
156 0 : sNumber = pTxtNd->GetNumString( false );
157 :
158 0 : SwNumRule* pRule( pTxtNd->GetNumRule() );
159 0 : if ( pTxtNd->IsCountedInList() && pRule )
160 : {
161 0 : const SwNumFmt& rNFmt = pRule->Get( static_cast<sal_uInt16>(pTxtNd->GetActualListLevel()) );
162 0 : sPost = rNFmt.GetSuffix();
163 0 : sPre = rNFmt.GetPrefix();
164 : }
165 : else
166 0 : sPost = aEmptyStr, sPre = aEmptyStr;
167 : }
168 : else
169 : {
170 0 : sPost = aEmptyStr;
171 0 : sPre = aEmptyStr;
172 0 : sNumber = String("??", RTL_TEXTENCODING_ASCII_US);
173 : }
174 :
175 0 : sTitle = pTxtNd->GetExpandTxt();
176 :
177 0 : for( xub_StrLen i = 0; i < sTitle.Len(); ++i )
178 0 : if( ' ' > sTitle.GetChar( i ) )
179 0 : sTitle.Erase( i--, 1 );
180 : }
181 : else
182 : {
183 0 : sNumber = aEmptyStr;
184 0 : sTitle = aEmptyStr;
185 0 : sPost = aEmptyStr;
186 0 : sPre = aEmptyStr;
187 : }
188 0 : }
189 :
190 0 : bool SwChapterField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
191 : {
192 0 : switch( nWhichId )
193 : {
194 : case FIELD_PROP_BYTE1:
195 0 : rAny <<= (sal_Int8)nLevel;
196 0 : break;
197 :
198 : case FIELD_PROP_USHORT1:
199 : {
200 : sal_Int16 nRet;
201 0 : switch( GetFormat() )
202 : {
203 0 : case CF_NUMBER: nRet = text::ChapterFormat::NUMBER; break;
204 0 : case CF_TITLE: nRet = text::ChapterFormat::NAME; break;
205 : case CF_NUMBER_NOPREPST:
206 0 : nRet = text::ChapterFormat::DIGIT;
207 0 : break;
208 : case CF_NUM_NOPREPST_TITLE:
209 0 : nRet = text::ChapterFormat::NO_PREFIX_SUFFIX;
210 0 : break;
211 : case CF_NUM_TITLE:
212 0 : default: nRet = text::ChapterFormat::NAME_NUMBER;
213 : }
214 0 : rAny <<= nRet;
215 : }
216 0 : break;
217 :
218 : default:
219 : OSL_FAIL("illegal property");
220 : }
221 0 : return true;
222 : }
223 :
224 0 : bool SwChapterField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
225 : {
226 0 : sal_Bool bRet = sal_True;
227 0 : switch( nWhichId )
228 : {
229 : case FIELD_PROP_BYTE1:
230 : {
231 0 : sal_Int8 nTmp = 0;
232 0 : rAny >>= nTmp;
233 0 : if(nTmp >= 0 && nTmp < MAXLEVEL)
234 0 : nLevel = nTmp;
235 : else
236 0 : bRet = false;
237 : break;
238 : }
239 :
240 : case FIELD_PROP_USHORT1:
241 : {
242 0 : sal_Int16 nVal = 0;
243 0 : rAny >>= nVal;
244 0 : switch( nVal )
245 : {
246 0 : case text::ChapterFormat::NAME: SetFormat(CF_TITLE); break;
247 0 : case text::ChapterFormat::NUMBER: SetFormat(CF_NUMBER); break;
248 : case text::ChapterFormat::NO_PREFIX_SUFFIX:
249 0 : SetFormat(CF_NUM_NOPREPST_TITLE);
250 0 : break;
251 : case text::ChapterFormat::DIGIT:
252 0 : SetFormat(CF_NUMBER_NOPREPST);
253 0 : break;
254 :
255 0 : default: SetFormat(CF_NUM_TITLE);
256 : }
257 : }
258 0 : break;
259 :
260 : default:
261 : OSL_FAIL("illegal property");
262 0 : bRet = false;
263 : }
264 0 : return bRet;
265 : }
266 :
267 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|