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 <libxml/xmlwriter.h>
24 #include <o3tl/any.hxx>
26 #include <svl/numformat.hxx>
27 #include <unotools/charclass.hxx>
32 #include <IDocumentFieldsAccess.hxx>
33 #include <IDocumentUndoRedo.hxx>
34 #include <IDocumentState.hxx>
35 #include <unofldmid.h>
37 using namespace ::com::sun::star
;
42 * Returns the language used for float <-> string conversions in
45 LanguageType
GetFieldTypeLanguage()
47 return LANGUAGE_SYSTEM
;
53 SwUserField::SwUserField(SwUserFieldType
* pTyp
, sal_uInt16 nSub
, sal_uInt32 nFormat
)
54 : SwValueField(pTyp
, nFormat
),
59 OUString
SwUserField::ExpandImpl(SwRootFrame
const*const) const
61 if(!(m_nSubType
& nsSwExtendedSubType::SUB_INVISIBLE
))
62 return static_cast<SwUserFieldType
*>(GetTyp())->Expand(GetFormat(), m_nSubType
, GetLanguage());
67 std::unique_ptr
<SwField
> SwUserField::Copy() const
69 std::unique_ptr
<SwField
> pTmp(new SwUserField(static_cast<SwUserFieldType
*>(GetTyp()), m_nSubType
, GetFormat()));
70 pTmp
->SetAutomaticLanguage(IsAutomaticLanguage());
71 pTmp
->SetTitle(GetTitle());
75 OUString
SwUserField::GetFieldName() const
77 return SwFieldType::GetTypeStr(SwFieldTypesEnum::User
) +
78 " " + GetTyp()->GetName() + " = " +
79 static_cast<SwUserFieldType
*>(GetTyp())->GetContent();
82 double SwUserField::GetValue() const
84 return static_cast<SwUserFieldType
*>(GetTyp())->GetValue();
87 void SwUserField::SetValue( const double& rVal
)
89 static_cast<SwUserFieldType
*>(GetTyp())->SetValue(rVal
);
93 OUString
SwUserField::GetPar1() const
95 return static_cast<const SwUserFieldType
*>(GetTyp())->GetName();
99 OUString
SwUserField::GetPar2() const
101 return static_cast<SwUserFieldType
*>(GetTyp())->GetContent(GetFormat());
104 void SwUserField::SetPar2(const OUString
& rStr
)
106 static_cast<SwUserFieldType
*>(GetTyp())->SetContent(rStr
, GetFormat());
109 sal_uInt16
SwUserField::GetSubType() const
111 return static_cast<SwUserFieldType
*>(GetTyp())->GetType() | m_nSubType
;
114 void SwUserField::SetSubType(sal_uInt16 nSub
)
116 static_cast<SwUserFieldType
*>(GetTyp())->SetType(nSub
& 0x00ff);
117 m_nSubType
= nSub
& 0xff00;
120 bool SwUserField::QueryValue( uno::Any
& rAny
, sal_uInt16 nWhichId
) const
124 case FIELD_PROP_BOOL2
:
125 rAny
<<= 0 != (m_nSubType
& nsSwExtendedSubType::SUB_CMD
);
127 case FIELD_PROP_BOOL1
:
128 rAny
<<= 0 == (m_nSubType
& nsSwExtendedSubType::SUB_INVISIBLE
);
130 case FIELD_PROP_FORMAT
:
131 rAny
<<= static_cast<sal_Int32
>(GetFormat());
134 return SwField::QueryValue(rAny
, nWhichId
);
139 bool SwUserField::PutValue( const uno::Any
& rAny
, sal_uInt16 nWhichId
)
143 case FIELD_PROP_BOOL1
:
144 if(*o3tl::doAccess
<bool>(rAny
))
145 m_nSubType
&= (~nsSwExtendedSubType::SUB_INVISIBLE
);
147 m_nSubType
|= nsSwExtendedSubType::SUB_INVISIBLE
;
149 case FIELD_PROP_BOOL2
:
150 if(*o3tl::doAccess
<bool>(rAny
))
151 m_nSubType
|= nsSwExtendedSubType::SUB_CMD
;
153 m_nSubType
&= (~nsSwExtendedSubType::SUB_CMD
);
155 case FIELD_PROP_FORMAT
:
163 return SwField::PutValue(rAny
, nWhichId
);
168 void SwUserField::dumpAsXml(xmlTextWriterPtr pWriter
) const
170 (void)xmlTextWriterStartElement(pWriter
, BAD_CAST("SwUserField"));
171 (void)xmlTextWriterWriteAttribute(pWriter
, BAD_CAST("nSubType"), BAD_CAST(OString::number(m_nSubType
).getStr()));
172 SwValueField::dumpAsXml(pWriter
);
173 (void)xmlTextWriterEndElement(pWriter
);
176 SwUserFieldType::SwUserFieldType( SwDoc
* pDocPtr
, const OUString
& aNam
)
177 : SwValueFieldType( pDocPtr
, SwFieldIds::User
),
179 m_nType(nsSwGetSetExpType::GSE_STRING
)
181 m_bValidValue
= m_bDeleted
= false;
184 EnableFormat(false); // Do not use a Numberformatter for nsSwGetSetExpType::GSE_STRING
187 OUString
SwUserFieldType::Expand(sal_uInt32 nFormat
, sal_uInt16 nSubType
, LanguageType nLng
)
189 if((m_nType
& nsSwGetSetExpType::GSE_EXPR
) && !(nSubType
& nsSwExtendedSubType::SUB_CMD
))
192 return ExpandValue(m_nValue
, nFormat
, nLng
);
195 EnableFormat(false); // Do not use a Numberformatter
199 std::unique_ptr
<SwFieldType
> SwUserFieldType::Copy() const
201 std::unique_ptr
<SwUserFieldType
> pTmp(new SwUserFieldType( GetDoc(), m_aName
));
202 pTmp
->m_aContent
= m_aContent
;
203 pTmp
->m_aContentLang
= m_aContentLang
;
204 pTmp
->m_nType
= m_nType
;
205 pTmp
->m_bValidValue
= m_bValidValue
;
206 pTmp
->m_nValue
= m_nValue
;
207 pTmp
->m_bDeleted
= m_bDeleted
;
212 OUString
SwUserFieldType::GetName() const
217 void SwUserFieldType::SwClientNotify(const SwModify
&, const SfxHint
& rHint
)
219 if (rHint
.GetId() == SfxHintId::SwLegacyModify
)
221 auto pLegacy
= static_cast<const sw::LegacyModifyHint
*>(&rHint
);
222 if (!pLegacy
->m_pOld
&& !pLegacy
->m_pNew
)
223 m_bValidValue
= false;
226 CallSwClientNotify(rHint
);
227 // update input fields that might be connected to the user field
228 if (!IsModifyLocked())
231 GetDoc()->getIDocumentFieldsAccess().GetSysFieldType(SwFieldIds::Input
)->UpdateFields();
236 double SwUserFieldType::GetValue( SwCalc
& rCalc
)
241 if(!rCalc
.Push( this ))
243 rCalc
.SetCalcError( SwCalcError::Syntax
);
247 // See if we need to temporarily switch rCalc's language: in case it
248 // differs from the field type locale.
249 const CharClass
* pCharClass
= rCalc
.GetCharClass();
250 LanguageTag aCharClassLanguage
= pCharClass
->getLanguageTag();
251 LanguageTag
aContentLang(m_aContentLang
);
253 // for the call of calculate we need the language that was used for putting/setting
254 // the m_aContent string, otherwise the aContent could be interpreted wrongly,
256 bool bSwitchLanguage
= m_aContentLang
!= aCharClassLanguage
.getBcp47();
259 rCalc
.SetCharClass(aContentLang
);
261 m_nValue
= rCalc
.Calculate( m_aContent
).GetDouble();
263 // we than have to set the proper char class languageTag again
266 rCalc
.SetCharClass(aCharClassLanguage
);
270 if( !rCalc
.IsCalcError() )
271 m_bValidValue
= true;
278 OUString
SwUserFieldType::GetInputOrDateTime( sal_uInt32 nFormat
) const
280 return static_cast<const SwValueFieldType
*>(this)->GetInputOrDateTime( m_aContent
, GetValue(), nFormat
);
283 OUString
SwUserFieldType::GetContent( sal_uInt32 nFormat
) const
285 if (nFormat
&& nFormat
!= SAL_MAX_UINT32
)
287 OUString sFormattedValue
;
288 const Color
* pCol
= nullptr;
290 SvNumberFormatter
* pFormatter
= GetDoc()->GetNumberFormatter();
292 pFormatter
->GetOutputString(GetValue(), nFormat
, sFormattedValue
, &pCol
);
293 return sFormattedValue
;
299 void SwUserFieldType::SetContent( const OUString
& rStr
, sal_uInt32 nFormat
)
301 if( m_aContent
== rStr
)
306 if (nFormat
&& nFormat
!= SAL_MAX_UINT32
)
310 if (GetDoc()->IsNumberFormat(rStr
, nFormat
, fValue
))
313 LanguageTag
aContentLanguage(GetFieldTypeLanguage());
314 m_aContentLang
= aContentLanguage
.getBcp47();
315 m_aContent
= DoubleToString(fValue
, aContentLanguage
.getLanguageType());
319 bool bModified
= GetDoc()->getIDocumentState().IsModified();
320 GetDoc()->getIDocumentState().SetModified();
321 if( !bModified
) // Bug 57028
323 GetDoc()->GetIDocumentUndoRedo().SetUndoNoResetModified();
327 void SwUserFieldType::QueryValue( uno::Any
& rAny
, sal_uInt16 nWhichId
) const
331 case FIELD_PROP_DOUBLE
:
334 case FIELD_PROP_PAR2
:
337 case FIELD_PROP_BOOL1
:
338 rAny
<<= 0 != (nsSwGetSetExpType::GSE_EXPR
&m_nType
);
345 void SwUserFieldType::PutValue( const uno::Any
& rAny
, sal_uInt16 nWhichId
)
349 case FIELD_PROP_DOUBLE
:
354 LanguageTag
aContentLanguage(GetFieldTypeLanguage());
355 m_aContentLang
= aContentLanguage
.getBcp47();
356 m_aContent
= DoubleToString(m_nValue
, aContentLanguage
.getLanguageType());
359 case FIELD_PROP_PAR2
:
362 case FIELD_PROP_BOOL1
:
363 if(*o3tl::doAccess
<bool>(rAny
))
365 m_nType
|= nsSwGetSetExpType::GSE_EXPR
;
366 m_nType
&= ~nsSwGetSetExpType::GSE_STRING
;
370 m_nType
&= ~nsSwGetSetExpType::GSE_EXPR
;
371 m_nType
|= nsSwGetSetExpType::GSE_STRING
;
379 void SwUserFieldType::EnsureValid()
383 SwCalc
aCalc(*GetDoc());
387 void SwUserFieldType::dumpAsXml(xmlTextWriterPtr pWriter
) const
389 (void)xmlTextWriterStartElement(pWriter
, BAD_CAST("SwUserFieldType"));
390 (void)xmlTextWriterWriteAttribute(pWriter
, BAD_CAST("nValue"), BAD_CAST(OString::number(m_nValue
).getStr()));
391 (void)xmlTextWriterWriteAttribute(pWriter
, BAD_CAST("aContent"), BAD_CAST(m_aContent
.toUtf8().getStr()));
392 (void)xmlTextWriterWriteAttribute(pWriter
, BAD_CAST("aContentLang"), BAD_CAST(m_aContentLang
.toUtf8().getStr()));
393 SwFieldType::dumpAsXml(pWriter
);
394 (void)xmlTextWriterEndElement(pWriter
);
397 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */