Clean up uses of Any::getValue() in sw
[LibreOffice.git] / sw / source / core / fields / usrfld.cxx
blob3c279dbe46919f860d8049fca7891a1d4e165fea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <o3tl/any.hxx>
24 #include <svl/zforlist.hxx>
25 #include <svl/zformat.hxx>
27 #include <svx/svdmodel.hxx>
29 #include <calbck.hxx>
30 #include <calc.hxx>
31 #include <usrfld.hxx>
32 #include <doc.hxx>
33 #include <IDocumentFieldsAccess.hxx>
34 #include <IDocumentUndoRedo.hxx>
35 #include <IDocumentState.hxx>
36 #include <editsh.hxx>
37 #include <dpage.hxx>
38 #include <unofldmid.h>
40 using namespace ::com::sun::star;
42 // Userfields
44 SwUserField::SwUserField(SwUserFieldType* pTyp, sal_uInt16 nSub, sal_uInt32 nFormat)
45 : SwValueField(pTyp, nFormat),
46 nSubType(nSub)
50 OUString SwUserField::Expand() const
52 if(!(nSubType & nsSwExtendedSubType::SUB_INVISIBLE))
53 return static_cast<SwUserFieldType*>(GetTyp())->Expand(GetFormat(), nSubType, GetLanguage());
55 return OUString();
58 SwField* SwUserField::Copy() const
60 SwField* pTmp = new SwUserField(static_cast<SwUserFieldType*>(GetTyp()), nSubType, GetFormat());
61 pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
62 return pTmp;
65 OUString SwUserField::GetFieldName() const
67 return SwFieldType::GetTypeStr(TYP_USERFLD) +
68 " " + GetTyp()->GetName() + " = " +
69 static_cast<SwUserFieldType*>(GetTyp())->GetContent();
72 double SwUserField::GetValue() const
74 return static_cast<SwUserFieldType*>(GetTyp())->GetValue();
77 void SwUserField::SetValue( const double& rVal )
79 static_cast<SwUserFieldType*>(GetTyp())->SetValue(rVal);
82 /// Get name
83 OUString SwUserField::GetPar1() const
85 return static_cast<const SwUserFieldType*>(GetTyp())->GetName();
88 /// Get content
89 OUString SwUserField::GetPar2() const
91 return static_cast<SwUserFieldType*>(GetTyp())->GetContent(GetFormat());
94 void SwUserField::SetPar2(const OUString& rStr)
96 static_cast<SwUserFieldType*>(GetTyp())->SetContent(rStr, GetFormat());
99 sal_uInt16 SwUserField::GetSubType() const
101 return static_cast<SwUserFieldType*>(GetTyp())->GetType() | nSubType;
104 void SwUserField::SetSubType(sal_uInt16 nSub)
106 static_cast<SwUserFieldType*>(GetTyp())->SetType(nSub & 0x00ff);
107 nSubType = nSub & 0xff00;
110 bool SwUserField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
112 switch( nWhichId )
114 case FIELD_PROP_BOOL2:
115 rAny <<= 0 != (nSubType & nsSwExtendedSubType::SUB_CMD);
116 break;
117 case FIELD_PROP_BOOL1:
118 rAny <<= 0 == (nSubType & nsSwExtendedSubType::SUB_INVISIBLE);
119 break;
120 case FIELD_PROP_FORMAT:
121 rAny <<= (sal_Int32)GetFormat();
122 break;
123 default:
124 return SwField::QueryValue(rAny, nWhichId);
126 return true;
129 bool SwUserField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
131 switch( nWhichId )
133 case FIELD_PROP_BOOL1:
134 if(*o3tl::doAccess<bool>(rAny))
135 nSubType &= (~nsSwExtendedSubType::SUB_INVISIBLE);
136 else
137 nSubType |= nsSwExtendedSubType::SUB_INVISIBLE;
138 break;
139 case FIELD_PROP_BOOL2:
140 if(*o3tl::doAccess<bool>(rAny))
141 nSubType |= nsSwExtendedSubType::SUB_CMD;
142 else
143 nSubType &= (~nsSwExtendedSubType::SUB_CMD);
144 break;
145 case FIELD_PROP_FORMAT:
147 sal_Int32 nTmp = 0;
148 rAny >>= nTmp;
149 SetFormat(nTmp);
151 break;
152 default:
153 return SwField::PutValue(rAny, nWhichId);
155 return true;
158 SwUserFieldType::SwUserFieldType( SwDoc* pDocPtr, const OUString& aNam )
159 : SwValueFieldType( pDocPtr, RES_USERFLD ),
160 nValue( 0 ),
161 nType(nsSwGetSetExpType::GSE_STRING)
163 bValidValue = bDeleted = false;
164 aName = aNam;
166 if (nType & nsSwGetSetExpType::GSE_STRING)
167 EnableFormat(false); // Do not use a Numberformatter
170 OUString SwUserFieldType::Expand(sal_uInt32 nFormat, sal_uInt16 nSubType, sal_uInt16 nLng)
172 if((nType & nsSwGetSetExpType::GSE_EXPR) && !(nSubType & nsSwExtendedSubType::SUB_CMD))
174 EnableFormat();
175 return ExpandValue(nValue, nFormat, nLng);
178 EnableFormat(false); // Do not use a Numberformatter
179 return aContent;
182 SwFieldType* SwUserFieldType::Copy() const
184 SwUserFieldType *pTmp = new SwUserFieldType( GetDoc(), aName );
185 pTmp->aContent = aContent;
186 pTmp->nType = nType;
187 pTmp->bValidValue = bValidValue;
188 pTmp->nValue = nValue;
189 pTmp->bDeleted = bDeleted;
191 return pTmp;
194 OUString SwUserFieldType::GetName() const
196 return aName;
199 void SwUserFieldType::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
201 if( !pOld && !pNew )
202 ChgValid( false );
204 NotifyClients( pOld, pNew );
206 // update input fields that might be connected to the user field
207 if ( !IsModifyLocked() )
209 LockModify();
210 GetDoc()->getIDocumentFieldsAccess().GetSysFieldType( RES_INPUTFLD )->UpdateFields();
211 UnlockModify();
215 double SwUserFieldType::GetValue( SwCalc& rCalc )
217 if(bValidValue)
218 return nValue;
220 if(!rCalc.Push( this ))
222 rCalc.SetCalcError( CALC_SYNTAX );
223 return 0;
225 nValue = rCalc.Calculate( aContent ).GetDouble();
226 rCalc.Pop();
228 if( !rCalc.IsCalcError() )
229 bValidValue = true;
230 else
231 nValue = 0;
233 return nValue;
236 OUString SwUserFieldType::GetContent( sal_uInt32 nFormat )
238 if (nFormat && nFormat != SAL_MAX_UINT32)
240 OUString sFormattedValue;
241 Color* pCol = nullptr;
243 SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
245 pFormatter->GetOutputString(GetValue(), nFormat, sFormattedValue, &pCol);
246 return sFormattedValue;
249 return aContent;
252 void SwUserFieldType::SetContent( const OUString& rStr, sal_uInt32 nFormat )
254 if( aContent != rStr )
256 aContent = rStr;
258 if (nFormat && nFormat != SAL_MAX_UINT32)
260 double fValue;
262 SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
264 if (pFormatter->IsNumberFormat(rStr, nFormat, fValue))
266 SetValue(fValue);
267 aContent = DoubleToString(fValue, nFormat);
271 bool bModified = GetDoc()->getIDocumentState().IsModified();
272 GetDoc()->getIDocumentState().SetModified();
273 if( !bModified ) // Bug 57028
275 GetDoc()->GetIDocumentUndoRedo().SetUndoNoResetModified();
280 bool SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
282 switch( nWhichId )
284 case FIELD_PROP_DOUBLE:
285 rAny <<= (double) nValue;
286 break;
287 case FIELD_PROP_PAR2:
288 rAny <<= aContent;
289 break;
290 case FIELD_PROP_BOOL1:
291 rAny <<= 0 != (nsSwGetSetExpType::GSE_EXPR&nType);
292 break;
293 default:
294 assert(false);
296 return true;
299 bool SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
301 switch( nWhichId )
303 case FIELD_PROP_DOUBLE:
305 double fVal = 0;
306 rAny >>= fVal;
307 nValue = fVal;
309 // The following line is in fact wrong, since the language is unknown (is part of the
310 // field) and, thus, aContent should also belong to the field. Each field can have a
311 // different language, but the same content with just different formatting.
312 aContent = DoubleToString(nValue, static_cast<sal_uInt32>(LANGUAGE_SYSTEM));
314 break;
315 case FIELD_PROP_PAR2:
316 rAny >>= aContent;
317 break;
318 case FIELD_PROP_BOOL1:
319 if(*o3tl::doAccess<bool>(rAny))
321 nType |= nsSwGetSetExpType::GSE_EXPR;
322 nType &= ~nsSwGetSetExpType::GSE_STRING;
324 else
326 nType &= ~nsSwGetSetExpType::GSE_EXPR;
327 nType |= nsSwGetSetExpType::GSE_STRING;
329 break;
330 default:
331 assert(false);
333 return true;
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */