1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
33 #include <IDocumentFieldsAccess.hxx>
34 #include <IDocumentUndoRedo.hxx>
35 #include <IDocumentState.hxx>
38 #include <unofldmid.h>
40 using namespace ::com::sun::star
;
44 SwUserField::SwUserField(SwUserFieldType
* pTyp
, sal_uInt16 nSub
, sal_uInt32 nFormat
)
45 : SwValueField(pTyp
, nFormat
),
50 OUString
SwUserField::Expand() const
52 if(!(nSubType
& nsSwExtendedSubType::SUB_INVISIBLE
))
53 return static_cast<SwUserFieldType
*>(GetTyp())->Expand(GetFormat(), nSubType
, GetLanguage());
58 SwField
* SwUserField::Copy() const
60 SwField
* pTmp
= new SwUserField(static_cast<SwUserFieldType
*>(GetTyp()), nSubType
, GetFormat());
61 pTmp
->SetAutomaticLanguage(IsAutomaticLanguage());
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
);
83 OUString
SwUserField::GetPar1() const
85 return static_cast<const SwUserFieldType
*>(GetTyp())->GetName();
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
114 case FIELD_PROP_BOOL2
:
115 rAny
<<= 0 != (nSubType
& nsSwExtendedSubType::SUB_CMD
);
117 case FIELD_PROP_BOOL1
:
118 rAny
<<= 0 == (nSubType
& nsSwExtendedSubType::SUB_INVISIBLE
);
120 case FIELD_PROP_FORMAT
:
121 rAny
<<= (sal_Int32
)GetFormat();
124 return SwField::QueryValue(rAny
, nWhichId
);
129 bool SwUserField::PutValue( const uno::Any
& rAny
, sal_uInt16 nWhichId
)
133 case FIELD_PROP_BOOL1
:
134 if(*o3tl::doAccess
<bool>(rAny
))
135 nSubType
&= (~nsSwExtendedSubType::SUB_INVISIBLE
);
137 nSubType
|= nsSwExtendedSubType::SUB_INVISIBLE
;
139 case FIELD_PROP_BOOL2
:
140 if(*o3tl::doAccess
<bool>(rAny
))
141 nSubType
|= nsSwExtendedSubType::SUB_CMD
;
143 nSubType
&= (~nsSwExtendedSubType::SUB_CMD
);
145 case FIELD_PROP_FORMAT
:
153 return SwField::PutValue(rAny
, nWhichId
);
158 SwUserFieldType::SwUserFieldType( SwDoc
* pDocPtr
, const OUString
& aNam
)
159 : SwValueFieldType( pDocPtr
, RES_USERFLD
),
161 nType(nsSwGetSetExpType::GSE_STRING
)
163 bValidValue
= bDeleted
= false;
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
))
175 return ExpandValue(nValue
, nFormat
, nLng
);
178 EnableFormat(false); // Do not use a Numberformatter
182 SwFieldType
* SwUserFieldType::Copy() const
184 SwUserFieldType
*pTmp
= new SwUserFieldType( GetDoc(), aName
);
185 pTmp
->aContent
= aContent
;
187 pTmp
->bValidValue
= bValidValue
;
188 pTmp
->nValue
= nValue
;
189 pTmp
->bDeleted
= bDeleted
;
194 OUString
SwUserFieldType::GetName() const
199 void SwUserFieldType::Modify( const SfxPoolItem
* pOld
, const SfxPoolItem
* pNew
)
204 NotifyClients( pOld
, pNew
);
206 // update input fields that might be connected to the user field
207 if ( !IsModifyLocked() )
210 GetDoc()->getIDocumentFieldsAccess().GetSysFieldType( RES_INPUTFLD
)->UpdateFields();
215 double SwUserFieldType::GetValue( SwCalc
& rCalc
)
220 if(!rCalc
.Push( this ))
222 rCalc
.SetCalcError( CALC_SYNTAX
);
225 nValue
= rCalc
.Calculate( aContent
).GetDouble();
228 if( !rCalc
.IsCalcError() )
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
;
252 void SwUserFieldType::SetContent( const OUString
& rStr
, sal_uInt32 nFormat
)
254 if( aContent
!= rStr
)
258 if (nFormat
&& nFormat
!= SAL_MAX_UINT32
)
262 SvNumberFormatter
* pFormatter
= GetDoc()->GetNumberFormatter();
264 if (pFormatter
->IsNumberFormat(rStr
, nFormat
, 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
284 case FIELD_PROP_DOUBLE
:
285 rAny
<<= (double) nValue
;
287 case FIELD_PROP_PAR2
:
290 case FIELD_PROP_BOOL1
:
291 rAny
<<= 0 != (nsSwGetSetExpType::GSE_EXPR
&nType
);
299 bool SwUserFieldType::PutValue( const uno::Any
& rAny
, sal_uInt16 nWhichId
)
303 case FIELD_PROP_DOUBLE
:
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
));
315 case FIELD_PROP_PAR2
:
318 case FIELD_PROP_BOOL1
:
319 if(*o3tl::doAccess
<bool>(rAny
))
321 nType
|= nsSwGetSetExpType::GSE_EXPR
;
322 nType
&= ~nsSwGetSetExpType::GSE_STRING
;
326 nType
&= ~nsSwGetSetExpType::GSE_EXPR
;
327 nType
|= nsSwGetSetExpType::GSE_STRING
;
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */