LibreOffice Module sw (master)  1
usrfld.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <libxml/xmlwriter.h>
23 
24 #include <o3tl/any.hxx>
25 
26 #include <svl/numformat.hxx>
27 #include <svl/zforlist.hxx>
28 #include <unotools/charclass.hxx>
29 
30 #include <calc.hxx>
31 #include <usrfld.hxx>
32 #include <doc.hxx>
34 #include <IDocumentUndoRedo.hxx>
35 #include <IDocumentState.hxx>
36 #include <unofldmid.h>
37 
38 using namespace ::com::sun::star;
39 
40 namespace
41 {
46 LanguageType GetFieldTypeLanguage()
47 {
48  return LANGUAGE_SYSTEM;
49 }
50 }
51 
52 // Userfields
53 
54 SwUserField::SwUserField(SwUserFieldType* pTyp, sal_uInt16 nSub, sal_uInt32 nFormat)
55  : SwValueField(pTyp, nFormat),
56  m_nSubType(nSub)
57 {
58 }
59 
60 OUString SwUserField::ExpandImpl(SwRootFrame const*const) const
61 {
63  return static_cast<SwUserFieldType*>(GetTyp())->Expand(GetFormat(), m_nSubType, GetLanguage());
64 
65  return OUString();
66 }
67 
68 std::unique_ptr<SwField> SwUserField::Copy() const
69 {
70  std::unique_ptr<SwField> pTmp(new SwUserField(static_cast<SwUserFieldType*>(GetTyp()), m_nSubType, GetFormat()));
71  pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
72  return pTmp;
73 }
74 
75 OUString SwUserField::GetFieldName() const
76 {
78  " " + GetTyp()->GetName() + " = " +
79  static_cast<SwUserFieldType*>(GetTyp())->GetContent();
80 }
81 
82 double SwUserField::GetValue() const
83 {
84  return static_cast<SwUserFieldType*>(GetTyp())->GetValue();
85 }
86 
87 void SwUserField::SetValue( const double& rVal )
88 {
89  static_cast<SwUserFieldType*>(GetTyp())->SetValue(rVal);
90 }
91 
93 OUString SwUserField::GetPar1() const
94 {
95  return static_cast<const SwUserFieldType*>(GetTyp())->GetName();
96 }
97 
99 OUString SwUserField::GetPar2() const
100 {
101  return static_cast<SwUserFieldType*>(GetTyp())->GetContent(GetFormat());
102 }
103 
104 void SwUserField::SetPar2(const OUString& rStr)
105 {
106  static_cast<SwUserFieldType*>(GetTyp())->SetContent(rStr, GetFormat());
107 }
108 
109 sal_uInt16 SwUserField::GetSubType() const
110 {
111  return static_cast<SwUserFieldType*>(GetTyp())->GetType() | m_nSubType;
112 }
113 
114 void SwUserField::SetSubType(sal_uInt16 nSub)
115 {
116  static_cast<SwUserFieldType*>(GetTyp())->SetType(nSub & 0x00ff);
117  m_nSubType = nSub & 0xff00;
118 }
119 
120 bool SwUserField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
121 {
122  switch( nWhichId )
123  {
124  case FIELD_PROP_BOOL2:
125  rAny <<= 0 != (m_nSubType & nsSwExtendedSubType::SUB_CMD);
126  break;
127  case FIELD_PROP_BOOL1:
129  break;
130  case FIELD_PROP_FORMAT:
131  rAny <<= static_cast<sal_Int32>(GetFormat());
132  break;
133  default:
134  return SwField::QueryValue(rAny, nWhichId);
135  }
136  return true;
137 }
138 
139 bool SwUserField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
140 {
141  switch( nWhichId )
142  {
143  case FIELD_PROP_BOOL1:
144  if(*o3tl::doAccess<bool>(rAny))
146  else
148  break;
149  case FIELD_PROP_BOOL2:
150  if(*o3tl::doAccess<bool>(rAny))
152  else
154  break;
155  case FIELD_PROP_FORMAT:
156  {
157  sal_Int32 nTmp = 0;
158  rAny >>= nTmp;
159  SetFormat(nTmp);
160  }
161  break;
162  default:
163  return SwField::PutValue(rAny, nWhichId);
164  }
165  return true;
166 }
167 
169 {
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);
174 }
175 
176 SwUserFieldType::SwUserFieldType( SwDoc* pDocPtr, const OUString& aNam )
177  : SwValueFieldType( pDocPtr, SwFieldIds::User ),
178  m_nValue( 0 ),
179  m_nType(nsSwGetSetExpType::GSE_STRING)
180 {
181  m_bValidValue = m_bDeleted = false;
182  m_aName = aNam;
183 
184  EnableFormat(false); // Do not use a Numberformatter for nsSwGetSetExpType::GSE_STRING
185 }
186 
187 OUString SwUserFieldType::Expand(sal_uInt32 nFormat, sal_uInt16 nSubType, LanguageType nLng)
188 {
190  {
191  EnableFormat();
192  return ExpandValue(m_nValue, nFormat, nLng);
193  }
194 
195  EnableFormat(false); // Do not use a Numberformatter
196  return m_aContent;
197 }
198 
199 std::unique_ptr<SwFieldType> SwUserFieldType::Copy() const
200 {
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;
208 
209  return pTmp;
210 }
211 
212 OUString SwUserFieldType::GetName() const
213 {
214  return m_aName;
215 }
216 
218 {
219  if (rHint.GetId() == SfxHintId::SwLegacyModify)
220  {
221  auto pLegacy = static_cast<const sw::LegacyModifyHint*>(&rHint);
222  if (!pLegacy->m_pOld && !pLegacy->m_pNew)
223  m_bValidValue = false;
224  }
225 
226  CallSwClientNotify(rHint);
227  // update input fields that might be connected to the user field
228  if (!IsModifyLocked())
229  {
230  LockModify();
232  UnlockModify();
233  }
234 }
235 
237 {
238  if(m_bValidValue)
239  return m_nValue;
240 
241  if(!rCalc.Push( this ))
242  {
244  return 0;
245  }
246 
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);
252 
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,
255 
256  bool bSwitchLanguage = m_aContentLang != aCharClassLanguage.getBcp47();
257 
258  if (bSwitchLanguage)
259  rCalc.SetCharClass(aContentLang);
260 
261  m_nValue = rCalc.Calculate( m_aContent ).GetDouble();
262 
263  // we than have to set the proper char class languageTag again
264 
265  if (bSwitchLanguage)
266  rCalc.SetCharClass(aCharClassLanguage);
267 
268  rCalc.Pop();
269 
270  if( !rCalc.IsCalcError() )
271  m_bValidValue = true;
272  else
273  m_nValue = 0;
274 
275  return m_nValue;
276 }
277 
278 OUString SwUserFieldType::GetContent( sal_uInt32 nFormat )
279 {
280  if (nFormat && nFormat != SAL_MAX_UINT32)
281  {
282  OUString sFormattedValue;
283  const Color* pCol = nullptr;
284 
285  SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
286 
287  pFormatter->GetOutputString(GetValue(), nFormat, sFormattedValue, &pCol);
288  return sFormattedValue;
289  }
290 
291  return m_aContent;
292 }
293 
294 void SwUserFieldType::SetContent( const OUString& rStr, sal_uInt32 nFormat )
295 {
296  if( m_aContent == rStr )
297  return;
298 
299  m_aContent = rStr;
300 
301  if (nFormat && nFormat != SAL_MAX_UINT32)
302  {
303  double fValue;
304 
305  if (GetDoc()->IsNumberFormat(rStr, nFormat, fValue))
306  {
307  SetValue(fValue);
308  LanguageTag aContentLanguage(GetFieldTypeLanguage());
309  m_aContentLang = aContentLanguage.getBcp47();
310  m_aContent = DoubleToString(fValue, nFormat);
311  }
312  }
313 
314  bool bModified = GetDoc()->getIDocumentState().IsModified();
316  if( !bModified ) // Bug 57028
317  {
319  }
320 }
321 
322 void SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
323 {
324  switch( nWhichId )
325  {
326  case FIELD_PROP_DOUBLE:
327  rAny <<= m_nValue;
328  break;
329  case FIELD_PROP_PAR2:
330  rAny <<= m_aContent;
331  break;
332  case FIELD_PROP_BOOL1:
333  rAny <<= 0 != (nsSwGetSetExpType::GSE_EXPR&m_nType);
334  break;
335  default:
336  assert(false);
337  }
338 }
339 
340 void SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
341 {
342  switch( nWhichId )
343  {
344  case FIELD_PROP_DOUBLE:
345  {
346  double fVal = 0;
347  rAny >>= fVal;
348  m_nValue = fVal;
349  LanguageTag aContentLanguage(GetFieldTypeLanguage());
350  m_aContentLang = aContentLanguage.getBcp47();
351  m_aContent = DoubleToString(m_nValue, static_cast<sal_uInt16>(GetFieldTypeLanguage()));
352  }
353  break;
354  case FIELD_PROP_PAR2:
355  rAny >>= m_aContent;
356  break;
357  case FIELD_PROP_BOOL1:
358  if(*o3tl::doAccess<bool>(rAny))
359  {
362  }
363  else
364  {
367  }
368  break;
369  default:
370  assert(false);
371  }
372 }
373 
375 {
376  (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwUserFieldType"));
377  (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("nValue"), BAD_CAST(OString::number(m_nValue).getStr()));
378  (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("aContent"), BAD_CAST(m_aContent.toUtf8().getStr()));
379  (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("aContentLang"), BAD_CAST(m_aContentLang.toUtf8().getStr()));
380  SwFieldType::dumpAsXml(pWriter);
381  (void)xmlTextWriterEndElement(pWriter);
382 }
383 
384 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 m_nType
Definition: usrfld.hxx:45
OUString m_aContentLang
Language used by m_aContents.
Definition: usrfld.hxx:44
Definition: calc.hxx:192
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: usrfld.cxx:60
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: usrfld.cxx:199
The shared part of a user field.
Definition: usrfld.hxx:34
virtual double GetValue() const override
Definition: usrfld.cxx:82
virtual OUString GetPar2() const override
Get content.
Definition: usrfld.cxx:99
virtual OUString GetName() const override
Only in derived classes.
Definition: usrfld.cxx:212
LanguageType GetLanguage() const
Language at field position.
Definition: fldbas.hxx:402
void EnableFormat(bool bFormat=true)
Definition: fldbas.hxx:429
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:397
SvNumberFormatter * GetNumberFormatter(bool bCreate=true)
Definition: doc.hxx:1411
const SwExtendedSubType SUB_INVISIBLE
Invisible.
Definition: fldbas.hxx:212
virtual void SetModified()=0
Must be called manually at changes of format.
bool IsCalcError() const
Definition: calc.hxx:247
bool m_bDeleted
Definition: usrfld.hxx:37
const OUString & getBcp47(bool bResolveSystem=true) const
Definition: doc.hxx:188
const LanguageTag & getLanguageTag() const
bool m_bValidValue
Definition: usrfld.hxx:36
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:144
static const OUString & GetTypeStr(SwFieldTypesEnum nTypeId)
Definition: fldbas.cxx:122
The root element of a Writer document layout.
Definition: rootfrm.hxx:82
SfxHintId GetId() const
OUString Expand(sal_uInt32 nFormat, sal_uInt16 nSubType, LanguageType nLng)
Definition: usrfld.cxx:187
#define FIELD_PROP_FORMAT
Definition: unofldmid.h:26
void UpdateFields() const
Definition: fldbas.cxx:149
#define SAL_MAX_UINT32
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:357
void Pop()
Definition: calc.cxx:618
void SetCharClass(const LanguageTag &rLanguageTag)
Definition: calc.cxx:630
double m_nValue
Float value type.
Definition: usrfld.hxx:39
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId)
Definition: fldbas.cxx:359
#define FIELD_PROP_DOUBLE
Definition: unofldmid.h:34
const SwGetSetExpType GSE_EXPR
Expression.
Definition: fldbas.hxx:203
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const
Definition: fldbas.cxx:346
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const override
Definition: usrfld.cxx:120
virtual OUString GetFieldName() const override
get name or content
Definition: usrfld.cxx:75
struct _xmlTextWriter * xmlTextWriterPtr
double GetDouble() const
Definition: calc.cxx:1452
virtual std::unique_ptr< SwField > Copy() const override
Definition: usrfld.cxx:68
OUString m_aContent
String value type.
Definition: usrfld.hxx:42
const SwExtendedSubType SUB_CMD
Show command.
Definition: fldbas.hxx:211
virtual bool IsModified() const =0
Changes of document?
virtual void SetUndoNoResetModified()=0
Disable (re)setting the document modified flag on Undo/Redo.
#define LANGUAGE_SYSTEM
#define FIELD_PROP_BOOL2
Definition: unofldmid.h:29
virtual void SetPar2(const OUString &rStr) override
Definition: usrfld.cxx:104
virtual void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: fldbas.cxx:160
SwFieldIds
Definition: fldbas.hxx:44
OUString m_aName
Definition: usrfld.hxx:40
void SetCalcError(SwCalcError eErr)
Definition: calc.hxx:246
void UnlockModify()
Definition: calbck.hxx:210
double GetValue() const
Definition: usrfld.hxx:81
void SetFormat(sal_uInt32 const nSet)
Definition: fldbas.hxx:303
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:394
void LockModify()
Definition: calbck.hxx:209
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: usrfld.cxx:374
SwDoc * GetDoc() const
Definition: fldbas.hxx:419
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:379
virtual void SetValue(const double &rVal) override
Definition: usrfld.cxx:87
SwFieldType * GetTyp() const
Definition: fldbas.hxx:392
virtual void SwClientNotify(const SwModify &, const SfxHint &) override
Definition: usrfld.cxx:217
virtual OUString GetPar1() const override
Get name.
Definition: usrfld.cxx:93
SwUserField(SwUserFieldType *, sal_uInt16 nSub, sal_uInt32 nFormat)
Definition: usrfld.cxx:54
#define FIELD_PROP_BOOL1
Definition: unofldmid.h:28
Fields containing values that have to be formatted via number formatter.
Definition: fldbas.hxx:408
SwSbxValue Calculate(const OUString &rStr)
Definition: calc.cxx:356
bool IsModifyLocked() const
Definition: calbck.hxx:211
virtual sal_uInt16 GetSubType() const override
Definition: usrfld.cxx:109
OUString ExpandValue(const double &rVal, sal_uInt32 nFormat, LanguageType nLng) const
return value formatted as string
Definition: fldbas.cxx:528
virtual OUString GetName() const
Only in derived classes.
Definition: fldbas.cxx:137
#define FIELD_PROP_PAR2
Definition: unofldmid.h:24
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: usrfld.cxx:168
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: fldbas.cxx:675
SwUserFieldType(SwDoc *pDocPtr, const OUString &)
Definition: usrfld.cxx:176
void SetValue(const double nVal)
Definition: usrfld.hxx:84
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId) override
Definition: usrfld.cxx:139
virtual void SetSubType(sal_uInt16 nSub) override
Definition: usrfld.cxx:114
OUString GetContent(sal_uInt32 nFormat=0)
Definition: usrfld.cxx:278
struct _ADOUser User
virtual void CallSwClientNotify(const SfxHint &rHint) const override
Definition: calbck.cxx:326
const CharClass * GetCharClass() const
Definition: calc.cxx:625
const SwGetSetExpType GSE_STRING
String.
Definition: fldbas.hxx:202
virtual SwFieldType * GetSysFieldType(const SwFieldIds eWhich) const =0
virtual void QueryValue(css::uno::Any &rVal, sal_uInt16 nMId) const override
Definition: usrfld.cxx:322
OUString DoubleToString(const double &rVal, LanguageType eLng) const
Definition: fldbas.cxx:589
bool Push(const SwUserFieldType *pUserFieldType)
Definition: calc.cxx:609
void GetOutputString(const double &fOutNumber, sal_uInt32 nFIndex, OUString &sOutString, const Color **ppColor, bool bUseStarFormat=false)
void SetContent(const OUString &rStr, sal_uInt32 nFormat=0)
Definition: usrfld.cxx:294
virtual void PutValue(const css::uno::Any &rVal, sal_uInt16 nMId) override
Definition: usrfld.cxx:340
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo
sal_uInt16 m_nSubType
Definition: usrfld.hxx:104