merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / fields / usrfld.cxx
blob870881dc2112c74c8f74ecb444a5ed1abf939f62
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: usrfld.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 <svtools/zforlist.hxx>
36 #include <svtools/zformat.hxx>
37 #include <svx/svdmodel.hxx>
40 #include <calbck.hxx>
41 #include <calc.hxx>
42 #include <usrfld.hxx>
43 #include <doc.hxx>
44 #include <editsh.hxx>
45 #include <dpage.hxx>
46 #ifndef _UNOFLDMID_H
47 #include <unofldmid.h>
48 #endif
50 using namespace ::com::sun::star;
51 using ::rtl::OUString;
52 /*--------------------------------------------------------------------
53 Beschreibung: Benutzerfelder
54 --------------------------------------------------------------------*/
56 SwUserField::SwUserField(SwUserFieldType* pTyp, sal_uInt16 nSub, sal_uInt32 nFmt)
57 : SwValueField(pTyp, nFmt),
58 nSubType(nSub)
62 String SwUserField::Expand() const
64 String sStr;
65 if(!(nSubType & nsSwExtendedSubType::SUB_INVISIBLE))
66 sStr = ((SwUserFieldType*)GetTyp())->Expand(GetFormat(), nSubType, GetLanguage());
68 return sStr;
71 SwField* SwUserField::Copy() const
73 SwField* pTmp = new SwUserField((SwUserFieldType*)GetTyp(), nSubType, GetFormat());
74 pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
75 return pTmp;
78 String SwUserField::GetCntnt(sal_Bool bName) const
80 if ( bName )
81 { String aStr(SwFieldType::GetTypeStr(TYP_USERFLD));
82 aStr += ' ';
83 aStr += GetTyp()->GetName();
84 aStr.AppendAscii(" = ");
85 aStr += ((SwUserFieldType*)GetTyp())->GetContent();
86 return aStr;
88 return Expand();
91 double SwUserField::GetValue() const
93 return ((SwUserFieldType*)GetTyp())->GetValue();
96 void SwUserField::SetValue( const double& rVal )
98 ((SwUserFieldType*)GetTyp())->SetValue(rVal);
101 /*--------------------------------------------------------------------
102 Beschreibung: Name
103 --------------------------------------------------------------------*/
105 const String& SwUserField::GetPar1() const
107 return ((SwUserFieldType*)GetTyp())->GetName();
110 /*--------------------------------------------------------------------
111 Beschreibung: Content
112 --------------------------------------------------------------------*/
114 String SwUserField::GetPar2() const
116 return ((SwUserFieldType*)GetTyp())->GetContent(GetFormat());
119 void SwUserField::SetPar2(const String& rStr)
121 ((SwUserFieldType*)GetTyp())->SetContent(rStr, GetFormat());
124 sal_uInt16 SwUserField::GetSubType() const
126 return ((SwUserFieldType*)GetTyp())->GetType() | nSubType;
129 void SwUserField::SetSubType(sal_uInt16 nSub)
131 ((SwUserFieldType*)GetTyp())->SetType(nSub & 0x00ff);
132 nSubType = nSub & 0xff00;
135 /*-----------------09.03.98 08:04-------------------
137 --------------------------------------------------*/
138 BOOL SwUserField::QueryValue( uno::Any& rAny, USHORT nWhichId ) const
140 switch( nWhichId )
142 case FIELD_PROP_BOOL2:
144 BOOL bTmp = 0 != (nSubType & nsSwExtendedSubType::SUB_CMD);
145 rAny.setValue(&bTmp, ::getBooleanCppuType());
147 break;
148 case FIELD_PROP_BOOL1:
150 BOOL bTmp = 0 == (nSubType & nsSwExtendedSubType::SUB_INVISIBLE);
151 rAny.setValue(&bTmp, ::getBooleanCppuType());
153 break;
154 case FIELD_PROP_FORMAT:
155 rAny <<= (sal_Int32)GetFormat();
156 break;
157 default:
158 return SwField::QueryValue(rAny, nWhichId);
160 return sal_True;
162 /*-----------------09.03.98 08:04-------------------
164 --------------------------------------------------*/
165 sal_Bool SwUserField::PutValue( const uno::Any& rAny, USHORT nWhichId )
167 switch( nWhichId )
169 case FIELD_PROP_BOOL1:
170 if(*(sal_Bool*) rAny.getValue())
171 nSubType &= (~nsSwExtendedSubType::SUB_INVISIBLE);
172 else
173 nSubType |= nsSwExtendedSubType::SUB_INVISIBLE;
174 break;
175 case FIELD_PROP_BOOL2:
176 if(*(sal_Bool*) rAny.getValue())
177 nSubType |= nsSwExtendedSubType::SUB_CMD;
178 else
179 nSubType &= (~nsSwExtendedSubType::SUB_CMD);
180 break;
181 case FIELD_PROP_FORMAT:
183 sal_Int32 nTmp = 0;
184 rAny >>= nTmp;
185 SetFormat(nTmp);
187 break;
188 default:
189 return SwField::PutValue(rAny, nWhichId);
191 return sal_True;
194 /*--------------------------------------------------------------------
195 Beschreibung: Benutzerfeldtypen
196 --------------------------------------------------------------------*/
198 SwUserFieldType::SwUserFieldType( SwDoc* pDocPtr, const String& aNam )
199 : SwValueFieldType( pDocPtr, RES_USERFLD ),
200 nValue( 0 ),
201 nType(nsSwGetSetExpType::GSE_STRING)
203 bValidValue = bDeleted = sal_False;
204 aName = aNam;
206 if (nType & nsSwGetSetExpType::GSE_STRING)
207 EnableFormat(sal_False); // Numberformatter nicht einsetzen
210 String SwUserFieldType::Expand(sal_uInt32 nFmt, sal_uInt16 nSubType, sal_uInt16 nLng)
212 String aStr(aContent);
213 if((nType & nsSwGetSetExpType::GSE_EXPR) && !(nSubType & nsSwExtendedSubType::SUB_CMD))
215 EnableFormat(sal_True);
216 aStr = ExpandValue(nValue, nFmt, nLng);
218 else
219 EnableFormat(sal_False); // Numberformatter nicht einsetzen
221 return aStr;
224 SwFieldType* SwUserFieldType::Copy() const
226 SwUserFieldType *pTmp = new SwUserFieldType( GetDoc(), aName );
227 pTmp->aContent = aContent;
228 pTmp->nType = nType;
229 pTmp->bValidValue = bValidValue;
230 pTmp->nValue = nValue;
231 pTmp->bDeleted = bDeleted;
233 return pTmp;
236 const String& SwUserFieldType::GetName() const
238 return aName;
241 void SwUserFieldType::Modify( SfxPoolItem* pOld, SfxPoolItem* pNew )
243 if( !pOld && !pNew )
244 ChgValid( sal_False );
246 SwModify::Modify( pOld, pNew );
247 // und ggfs. am UserFeld haengende InputFelder updaten!
248 GetDoc()->GetSysFldType( RES_INPUTFLD )->UpdateFlds();
251 double SwUserFieldType::GetValue( SwCalc& rCalc )
253 if(bValidValue)
254 return nValue;
256 if(!rCalc.Push( this ))
258 rCalc.SetCalcError( CALC_SYNTAX );
259 return 0;
261 nValue = rCalc.Calculate( aContent ).GetDouble();
262 rCalc.Pop( this );
264 if( !rCalc.IsCalcError() )
265 bValidValue = sal_True;
266 else
267 nValue = 0;
269 return nValue;
272 String SwUserFieldType::GetContent( sal_uInt32 nFmt )
274 if (nFmt && nFmt != SAL_MAX_UINT32)
276 String sFormattedValue;
277 Color* pCol = 0;
279 SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
281 pFormatter->GetOutputString(GetValue(), nFmt, sFormattedValue, &pCol);
282 return sFormattedValue;
284 else
285 return aContent;
288 void SwUserFieldType::SetContent( const String& rStr, sal_uInt32 nFmt )
290 if( aContent != rStr )
292 aContent = rStr;
294 if (nFmt && nFmt != SAL_MAX_UINT32)
296 double fValue;
298 SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
300 if (pFormatter->IsNumberFormat(rStr, nFmt, fValue))
302 SetValue(fValue);
303 aContent.Erase();
304 DoubleToString(aContent, fValue, nFmt);
308 sal_Bool bModified = GetDoc()->IsModified();
309 GetDoc()->SetModified();
310 if( !bModified ) // Bug 57028
311 GetDoc()->SetUndoNoResetModified();
315 /*-----------------04.03.98 17:05-------------------
317 --------------------------------------------------*/
318 BOOL SwUserFieldType::QueryValue( uno::Any& rAny, USHORT nWhichId ) const
320 switch( nWhichId )
322 case FIELD_PROP_DOUBLE:
323 rAny <<= (double) nValue;
324 break;
325 case FIELD_PROP_PAR2:
326 rAny <<= rtl::OUString(aContent);
327 break;
328 case FIELD_PROP_BOOL1:
330 BOOL bExpression = 0 != (nsSwGetSetExpType::GSE_EXPR&nType);
331 rAny.setValue(&bExpression, ::getBooleanCppuType());
333 break;
334 default:
335 DBG_ERROR("illegal property");
337 return sal_True;
339 /*-----------------04.03.98 17:05-------------------
341 --------------------------------------------------*/
342 BOOL SwUserFieldType::PutValue( const uno::Any& rAny, USHORT nWhichId )
344 switch( nWhichId )
346 case FIELD_PROP_DOUBLE:
348 double fVal = 0;
349 rAny >>= fVal;
350 nValue = fVal;
352 // Folgende Zeile ist eigentlich falsch, da die Sprache unbekannt ist
353 // (haengt am Feld) und aContent daher auch eigentlich ans Feld gehoeren
354 // muesste. Jedes Feld kann eine andere Sprache, aber den gleichen Inhalt
355 // haben, nur die Formatierung ist unterschiedlich.
356 DoubleToString(aContent, nValue, (sal_uInt16)LANGUAGE_SYSTEM);
358 break;
359 case FIELD_PROP_PAR2:
360 ::GetString( rAny, aContent );
361 break;
362 case FIELD_PROP_BOOL1:
363 if(*(sal_Bool*)rAny.getValue())
365 nType |= nsSwGetSetExpType::GSE_EXPR;
366 nType &= ~nsSwGetSetExpType::GSE_STRING;
368 else
370 nType &= ~nsSwGetSetExpType::GSE_EXPR;
371 nType |= nsSwGetSetExpType::GSE_STRING;
373 break;
374 default:
375 DBG_ERROR("illegal property");
377 return sal_True;