android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / fields / usrfld.cxx
blobebfe46fdb9e9d5119b7fd8d32e3efbf02f340670
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 <libxml/xmlwriter.h>
24 #include <o3tl/any.hxx>
26 #include <svl/numformat.hxx>
27 #include <unotools/charclass.hxx>
29 #include <calc.hxx>
30 #include <usrfld.hxx>
31 #include <doc.hxx>
32 #include <IDocumentFieldsAccess.hxx>
33 #include <IDocumentUndoRedo.hxx>
34 #include <IDocumentState.hxx>
35 #include <unofldmid.h>
37 using namespace ::com::sun::star;
39 namespace
41 /**
42 * Returns the language used for float <-> string conversions in
43 * SwUserFieldType.
45 LanguageType GetFieldTypeLanguage()
47 return LANGUAGE_SYSTEM;
51 // Userfields
53 SwUserField::SwUserField(SwUserFieldType* pTyp, sal_uInt16 nSub, sal_uInt32 nFormat)
54 : SwValueField(pTyp, nFormat),
55 m_nSubType(nSub)
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());
64 return OUString();
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());
72 return pTmp;
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);
92 /// Get name
93 OUString SwUserField::GetPar1() const
95 return static_cast<const SwUserFieldType*>(GetTyp())->GetName();
98 /// Get content
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
122 switch( nWhichId )
124 case FIELD_PROP_BOOL2:
125 rAny <<= 0 != (m_nSubType & nsSwExtendedSubType::SUB_CMD);
126 break;
127 case FIELD_PROP_BOOL1:
128 rAny <<= 0 == (m_nSubType & nsSwExtendedSubType::SUB_INVISIBLE);
129 break;
130 case FIELD_PROP_FORMAT:
131 rAny <<= static_cast<sal_Int32>(GetFormat());
132 break;
133 default:
134 return SwField::QueryValue(rAny, nWhichId);
136 return true;
139 bool SwUserField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
141 switch( nWhichId )
143 case FIELD_PROP_BOOL1:
144 if(*o3tl::doAccess<bool>(rAny))
145 m_nSubType &= (~nsSwExtendedSubType::SUB_INVISIBLE);
146 else
147 m_nSubType |= nsSwExtendedSubType::SUB_INVISIBLE;
148 break;
149 case FIELD_PROP_BOOL2:
150 if(*o3tl::doAccess<bool>(rAny))
151 m_nSubType |= nsSwExtendedSubType::SUB_CMD;
152 else
153 m_nSubType &= (~nsSwExtendedSubType::SUB_CMD);
154 break;
155 case FIELD_PROP_FORMAT:
157 sal_Int32 nTmp = 0;
158 rAny >>= nTmp;
159 SetFormat(nTmp);
161 break;
162 default:
163 return SwField::PutValue(rAny, nWhichId);
165 return true;
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 ),
178 m_nValue( 0 ),
179 m_nType(nsSwGetSetExpType::GSE_STRING)
181 m_bValidValue = m_bDeleted = false;
182 m_aName = aNam;
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))
191 EnableFormat();
192 return ExpandValue(m_nValue, nFormat, nLng);
195 EnableFormat(false); // Do not use a Numberformatter
196 return m_aContent;
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;
209 return pTmp;
212 OUString SwUserFieldType::GetName() const
214 return m_aName;
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())
230 LockModify();
231 GetDoc()->getIDocumentFieldsAccess().GetSysFieldType(SwFieldIds::Input)->UpdateFields();
232 UnlockModify();
236 double SwUserFieldType::GetValue( SwCalc& rCalc )
238 if(m_bValidValue)
239 return m_nValue;
241 if(!rCalc.Push( this ))
243 rCalc.SetCalcError( SwCalcError::Syntax );
244 return 0;
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();
258 if (bSwitchLanguage)
259 rCalc.SetCharClass(aContentLang);
261 m_nValue = rCalc.Calculate( m_aContent ).GetDouble();
263 // we than have to set the proper char class languageTag again
265 if (bSwitchLanguage)
266 rCalc.SetCharClass(aCharClassLanguage);
268 rCalc.Pop();
270 if( !rCalc.IsCalcError() )
271 m_bValidValue = true;
272 else
273 m_nValue = 0;
275 return m_nValue;
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;
296 return m_aContent;
299 void SwUserFieldType::SetContent( const OUString& rStr, sal_uInt32 nFormat )
301 if( m_aContent == rStr )
302 return;
304 m_aContent = rStr;
306 if (nFormat && nFormat != SAL_MAX_UINT32)
308 double fValue;
310 if (GetDoc()->IsNumberFormat(rStr, nFormat, fValue))
312 SetValue(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
329 switch( nWhichId )
331 case FIELD_PROP_DOUBLE:
332 rAny <<= m_nValue;
333 break;
334 case FIELD_PROP_PAR2:
335 rAny <<= m_aContent;
336 break;
337 case FIELD_PROP_BOOL1:
338 rAny <<= 0 != (nsSwGetSetExpType::GSE_EXPR&m_nType);
339 break;
340 default:
341 assert(false);
345 void SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
347 switch( nWhichId )
349 case FIELD_PROP_DOUBLE:
351 double fVal = 0;
352 rAny >>= fVal;
353 m_nValue = fVal;
354 LanguageTag aContentLanguage(GetFieldTypeLanguage());
355 m_aContentLang = aContentLanguage.getBcp47();
356 m_aContent = DoubleToString(m_nValue, aContentLanguage.getLanguageType());
358 break;
359 case FIELD_PROP_PAR2:
360 rAny >>= m_aContent;
361 break;
362 case FIELD_PROP_BOOL1:
363 if(*o3tl::doAccess<bool>(rAny))
365 m_nType |= nsSwGetSetExpType::GSE_EXPR;
366 m_nType &= ~nsSwGetSetExpType::GSE_STRING;
368 else
370 m_nType &= ~nsSwGetSetExpType::GSE_EXPR;
371 m_nType |= nsSwGetSetExpType::GSE_STRING;
373 break;
374 default:
375 assert(false);
379 void SwUserFieldType::EnsureValid()
381 if(IsValid())
382 return;
383 SwCalc aCalc(*GetDoc());
384 GetValue(aCalc);
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: */