merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / fields / chpfld.cxx
blob15c2e23c010eb1aa50fee99fb3e47c4a669c1839
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: chpfld.cxx,v $
10 * $Revision: 1.19 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include <com/sun/star/text/ChapterFormat.hpp>
36 #include <doc.hxx>
37 #include <frame.hxx> // SwChapterFieldType::ChangeExpansion()
38 #include <pam.hxx> // fuer GetBodyTxtNode
39 #include <ndtxt.hxx>
40 #include <chpfld.hxx>
41 #include <expfld.hxx> // fuer GetBodyTxtNode
42 #ifndef _UNOFLDMID_H
43 #include <unofldmid.h>
44 #endif
45 #include <numrule.hxx>
47 using namespace ::com::sun::star;
49 /*--------------------------------------------------------------------
50 Beschreibung: SwChapterFieldType
51 --------------------------------------------------------------------*/
54 SwChapterFieldType::SwChapterFieldType()
55 : SwFieldType( RES_CHAPTERFLD )
60 SwFieldType* SwChapterFieldType::Copy() const
62 return new SwChapterFieldType();
66 /*--------------------------------------------------------------------
67 Beschreibung: Kapittelfeld
68 --------------------------------------------------------------------*/
71 SwChapterField::SwChapterField(SwChapterFieldType* pTyp, sal_uInt32 nFmt)
72 : SwField(pTyp, nFmt), nLevel( 0 )
76 String SwChapterField::Expand() const
78 String sStr( sNumber );
79 switch( GetFormat() )
81 case CF_TITLE: sStr = sTitle; break;
83 case CF_NUMBER:
84 case CF_NUM_TITLE: sStr.Insert( sPre, 0 );
85 sStr += sPost;
86 if( CF_NUM_TITLE == GetFormat() )
87 sStr += sTitle;
88 break;
90 case CF_NUM_NOPREPST_TITLE: sStr += sTitle; break;
92 return sStr;
96 SwField* SwChapterField::Copy() const
98 SwChapterField *pTmp =
99 new SwChapterField((SwChapterFieldType*)GetTyp(), GetFormat());
100 pTmp->nLevel = nLevel;
101 pTmp->sTitle = sTitle;
102 pTmp->sNumber = sNumber;
103 pTmp->sPost = sPost;
104 pTmp->sPre = sPre;
106 return pTmp;
109 // --> OD 2008-02-14 #i53420#
110 //void SwChapterField::ChangeExpansion( const SwFrm* pFrm,
111 // const SwTxtNode* pTxtNd,
112 // sal_Bool bSrchNum )
114 // ASSERT( pFrm, "in welchem Frame stehe ich denn?" )
115 // SwDoc* pDoc = (SwDoc*)pTxtNd->GetDoc();
116 // SwPosition aPos( pDoc->GetNodes().GetEndOfContent() );
118 // if( pFrm->IsInDocBody() )
119 // aPos.nNode = *pTxtNd;
120 // else if( 0 == (pTxtNd = GetBodyTxtNode( *pDoc, aPos, *pFrm )) )
121 // // kein TxtNode (Formatierung Kopf/Fusszeile)
122 // return;
123 // ChangeExpansion(*pTxtNd, bSrchNum);
125 void SwChapterField::ChangeExpansion(const SwFrm* pFrm,
126 const SwCntntNode* pCntntNode,
127 sal_Bool bSrchNum )
129 ASSERT( pFrm, "in welchem Frame stehe ich denn?" )
130 SwDoc* pDoc = (SwDoc*)pCntntNode->GetDoc();
132 const SwTxtNode* pTxtNode = dynamic_cast<const SwTxtNode*>(pCntntNode);
133 if ( !pTxtNode || !pFrm->IsInDocBody() )
135 SwPosition aDummyPos( pDoc->GetNodes().GetEndOfContent() );
136 pTxtNode = GetBodyTxtNode( *pDoc, aDummyPos, *pFrm );
139 if ( pTxtNode )
141 ChangeExpansion( *pTxtNode, bSrchNum );
144 // <--
146 void SwChapterField::ChangeExpansion(const SwTxtNode &rTxtNd, sal_Bool bSrchNum)
148 SwDoc* pDoc = (SwDoc*)rTxtNd.GetDoc();
149 const SwTxtNode *pTxtNd = rTxtNd.FindOutlineNodeOfLevel( nLevel );
150 if( pTxtNd )
152 if( bSrchNum )
154 const SwTxtNode* pONd = pTxtNd;
155 do {
156 if( pONd && pONd->GetTxtColl() )
158 BYTE nPrevLvl = nLevel;
160 // --> OD 2008-04-02 #refactorlists#
161 // nLevel = GetRealLevel( pONd->GetTxtColl()->
162 // GetOutlineLevel() );
163 //ASSERT( pONd->GetOutlineLevel() >= 0 && pONd->GetOutlineLevel() < MAXLEVEL, //#outline level,zhaojianwei
164 // "<SwChapterField::ChangeExpansion(..)> - outline node with inconsistent outline level. Serious defect -> please inform OD." );
165 //nLevel = static_cast<BYTE>(pONd->GetOutlineLevel());
166 ASSERT( pONd->GetAttrOutlineLevel() >= 0 && pONd->GetAttrOutlineLevel() <= MAXLEVEL,
167 "<SwChapterField::ChangeExpansion(..)> - outline node with inconsistent outline level. Serious defect -> please inform OD." );
168 nLevel = static_cast<BYTE>(pONd->GetAttrOutlineLevel()); //<-end,zhaojianwei
169 // <--
171 if( nPrevLvl < nLevel )
172 nLevel = nPrevLvl;
173 else if( SVX_NUM_NUMBER_NONE != pDoc->GetOutlineNumRule()
174 ->Get( nLevel ).GetNumberingType() )
176 pTxtNd = pONd;
177 break;
180 if( !nLevel-- )
181 break;
182 pONd = pTxtNd->FindOutlineNodeOfLevel( nLevel );
184 else
185 break;
186 } while( sal_True );
189 // nur die Nummer besorgen, ohne Pre-/Post-fixstrings
191 if ( pTxtNd->IsOutline() )
193 // --> OD 2005-11-17 #128041#
194 // correction of refactoring done by cws swnumtree:
195 // retrieve numbering string without prefix and suffix strings
196 // as stated in the above german comment.
197 sNumber = pTxtNd->GetNumString( false );
198 // <--
200 SwNumRule* pRule( pTxtNd->GetNumRule() );
201 if ( pTxtNd->IsCountedInList() && pRule )
203 const SwNumFmt& rNFmt = pRule->Get( static_cast<USHORT>(pTxtNd->GetActualListLevel()) );
204 sPost = rNFmt.GetSuffix();
205 sPre = rNFmt.GetPrefix();
207 else
208 sPost = aEmptyStr, sPre = aEmptyStr;
210 else
212 sPost = aEmptyStr;
213 sPre = aEmptyStr;
214 sNumber = String("??", RTL_TEXTENCODING_ASCII_US);
217 sTitle = pTxtNd->GetExpandTxt();
219 for( xub_StrLen i = 0; i < sTitle.Len(); ++i )
220 if( ' ' > sTitle.GetChar( i ) )
221 sTitle.Erase( i--, 1 );
223 else
225 sNumber = aEmptyStr;
226 sTitle = aEmptyStr;
227 sPost = aEmptyStr;
228 sPre = aEmptyStr;
232 /*-----------------05.03.98 16:19-------------------
234 --------------------------------------------------*/
235 BOOL SwChapterField::QueryValue( uno::Any& rAny, USHORT nWhichId ) const
237 switch( nWhichId )
239 case FIELD_PROP_BYTE1:
240 rAny <<= (sal_Int8)nLevel;
241 break;
243 case FIELD_PROP_USHORT1:
245 sal_Int16 nRet;
246 switch( GetFormat() )
248 case CF_NUMBER: nRet = text::ChapterFormat::NUMBER; break;
249 case CF_TITLE: nRet = text::ChapterFormat::NAME; break;
250 case CF_NUMBER_NOPREPST:
251 nRet = text::ChapterFormat::DIGIT;
252 break;
253 case CF_NUM_NOPREPST_TITLE:
254 nRet = text::ChapterFormat::NO_PREFIX_SUFFIX;
255 break;
256 case CF_NUM_TITLE:
257 default: nRet = text::ChapterFormat::NAME_NUMBER;
259 rAny <<= nRet;
261 break;
263 default:
264 DBG_ERROR("illegal property");
266 return sal_True;
268 /*-----------------05.03.98 16:19-------------------
270 --------------------------------------------------*/
271 BOOL SwChapterField::PutValue( const uno::Any& rAny, USHORT nWhichId )
273 BOOL bRet = TRUE;
274 switch( nWhichId )
276 case FIELD_PROP_BYTE1:
278 sal_Int8 nTmp = 0;
279 rAny >>= nTmp;
280 if(nTmp >= 0 && nTmp < MAXLEVEL)
281 nLevel = nTmp;
282 else
283 bRet = FALSE;
284 break;
287 case FIELD_PROP_USHORT1:
289 sal_Int16 nVal = 0;
290 rAny >>= nVal;
291 switch( nVal )
293 case text::ChapterFormat::NAME: SetFormat(CF_TITLE); break;
294 case text::ChapterFormat::NUMBER: SetFormat(CF_NUMBER); break;
295 case text::ChapterFormat::NO_PREFIX_SUFFIX:
296 SetFormat(CF_NUM_NOPREPST_TITLE);
297 break;
298 case text::ChapterFormat::DIGIT:
299 SetFormat(CF_NUMBER_NOPREPST);
300 break;
301 //case text::ChapterFormat::NAME_NUMBER:
302 default: SetFormat(CF_NUM_TITLE);
305 break;
307 default:
308 DBG_ERROR("illegal property");
309 bRet = FALSE;
311 return bRet;