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/zforlist.hxx>
27 #include <unotools/charclass.hxx>
28 
29 #include <calc.hxx>
30 #include <usrfld.hxx>
31 #include <doc.hxx>
33 #include <IDocumentUndoRedo.hxx>
34 #include <IDocumentState.hxx>
35 #include <unofldmid.h>
36 
37 using namespace ::com::sun::star;
38 
39 namespace
40 {
45 LanguageType GetFieldTypeLanguage()
46 {
47  return LANGUAGE_SYSTEM;
48 }
49 }
50 
51 // Userfields
52 
53 SwUserField::SwUserField(SwUserFieldType* pTyp, sal_uInt16 nSub, sal_uInt32 nFormat)
54  : SwValueField(pTyp, nFormat),
55  m_nSubType(nSub)
56 {
57 }
58 
59 OUString SwUserField::ExpandImpl(SwRootFrame const*const) const
60 {
62  return static_cast<SwUserFieldType*>(GetTyp())->Expand(GetFormat(), m_nSubType, GetLanguage());
63 
64  return OUString();
65 }
66 
67 std::unique_ptr<SwField> SwUserField::Copy() const
68 {
69  std::unique_ptr<SwField> pTmp(new SwUserField(static_cast<SwUserFieldType*>(GetTyp()), m_nSubType, GetFormat()));
70  pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
71  return pTmp;
72 }
73 
74 OUString SwUserField::GetFieldName() const
75 {
77  " " + GetTyp()->GetName() + " = " +
78  static_cast<SwUserFieldType*>(GetTyp())->GetContent();
79 }
80 
81 double SwUserField::GetValue() const
82 {
83  return static_cast<SwUserFieldType*>(GetTyp())->GetValue();
84 }
85 
86 void SwUserField::SetValue( const double& rVal )
87 {
88  static_cast<SwUserFieldType*>(GetTyp())->SetValue(rVal);
89 }
90 
92 OUString SwUserField::GetPar1() const
93 {
94  return static_cast<const SwUserFieldType*>(GetTyp())->GetName();
95 }
96 
98 OUString SwUserField::GetPar2() const
99 {
100  return static_cast<SwUserFieldType*>(GetTyp())->GetContent(GetFormat());
101 }
102 
103 void SwUserField::SetPar2(const OUString& rStr)
104 {
105  static_cast<SwUserFieldType*>(GetTyp())->SetContent(rStr, GetFormat());
106 }
107 
108 sal_uInt16 SwUserField::GetSubType() const
109 {
110  return static_cast<SwUserFieldType*>(GetTyp())->GetType() | m_nSubType;
111 }
112 
113 void SwUserField::SetSubType(sal_uInt16 nSub)
114 {
115  static_cast<SwUserFieldType*>(GetTyp())->SetType(nSub & 0x00ff);
116  m_nSubType = nSub & 0xff00;
117 }
118 
119 bool SwUserField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
120 {
121  switch( nWhichId )
122  {
123  case FIELD_PROP_BOOL2:
124  rAny <<= 0 != (m_nSubType & nsSwExtendedSubType::SUB_CMD);
125  break;
126  case FIELD_PROP_BOOL1:
128  break;
129  case FIELD_PROP_FORMAT:
130  rAny <<= static_cast<sal_Int32>(GetFormat());
131  break;
132  default:
133  return SwField::QueryValue(rAny, nWhichId);
134  }
135  return true;
136 }
137 
138 bool SwUserField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
139 {
140  switch( nWhichId )
141  {
142  case FIELD_PROP_BOOL1:
143  if(*o3tl::doAccess<bool>(rAny))
145  else
147  break;
148  case FIELD_PROP_BOOL2:
149  if(*o3tl::doAccess<bool>(rAny))
151  else
153  break;
154  case FIELD_PROP_FORMAT:
155  {
156  sal_Int32 nTmp = 0;
157  rAny >>= nTmp;
158  SetFormat(nTmp);
159  }
160  break;
161  default:
162  return SwField::PutValue(rAny, nWhichId);
163  }
164  return true;
165 }
166 
168 {
169  (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwUserField"));
170  (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("nSubType"), BAD_CAST(OString::number(m_nSubType).getStr()));
171  SwValueField::dumpAsXml(pWriter);
172  (void)xmlTextWriterEndElement(pWriter);
173 }
174 
175 SwUserFieldType::SwUserFieldType( SwDoc* pDocPtr, const OUString& aNam )
176  : SwValueFieldType( pDocPtr, SwFieldIds::User ),
177  m_nValue( 0 ),
178  m_nType(nsSwGetSetExpType::GSE_STRING)
179 {
180  m_bValidValue = m_bDeleted = false;
181  m_aName = aNam;
182 
183  EnableFormat(false); // Do not use a Numberformatter for nsSwGetSetExpType::GSE_STRING
184 }
185 
186 OUString SwUserFieldType::Expand(sal_uInt32 nFormat, sal_uInt16 nSubType, LanguageType nLng)
187 {
189  {
190  EnableFormat();
191  return ExpandValue(m_nValue, nFormat, nLng);
192  }
193 
194  EnableFormat(false); // Do not use a Numberformatter
195  return m_aContent;
196 }
197 
198 std::unique_ptr<SwFieldType> SwUserFieldType::Copy() const
199 {
200  std::unique_ptr<SwUserFieldType> pTmp(new SwUserFieldType( GetDoc(), m_aName ));
201  pTmp->m_aContent = m_aContent;
202  pTmp->m_aContentLang = m_aContentLang;
203  pTmp->m_nType = m_nType;
204  pTmp->m_bValidValue = m_bValidValue;
205  pTmp->m_nValue = m_nValue;
206  pTmp->m_bDeleted = m_bDeleted;
207 
208  return pTmp;
209 }
210 
211 OUString SwUserFieldType::GetName() const
212 {
213  return m_aName;
214 }
215 
217 {
218  auto pLegacy = dynamic_cast<const sw::LegacyModifyHint*>(&rHint);
219  if (pLegacy && !pLegacy->m_pOld && !pLegacy->m_pNew)
220  m_bValidValue = false;
221 
222  CallSwClientNotify(rHint);
223  // update input fields that might be connected to the user field
224  if (!IsModifyLocked())
225  {
226  LockModify();
228  UnlockModify();
229  }
230 }
231 
233 {
234  if(m_bValidValue)
235  return m_nValue;
236 
237  if(!rCalc.Push( this ))
238  {
240  return 0;
241  }
242 
243  // See if we need to temporarily switch rCalc's language: in case it
244  // differs from the field type locale.
245  CharClass* pCharClass = rCalc.GetCharClass();
246  LanguageTag aCharClassLanguage = pCharClass->getLanguageTag();
247  LanguageTag aContentLang(m_aContentLang);
248 
249  // for the call of calculate we need the language that was used for putting/setting
250  // the m_aContent string, otherwise the aContent could be interpreted wrongly,
251 
252  bool bSwitchLanguage = m_aContentLang != aCharClassLanguage.getBcp47();
253 
254  if (bSwitchLanguage)
255  pCharClass->setLanguageTag(aContentLang);
256 
257  m_nValue = rCalc.Calculate( m_aContent ).GetDouble();
258 
259  // we than have to set the proper char class languageTag again
260 
261  if (bSwitchLanguage)
262  pCharClass->setLanguageTag(aCharClassLanguage);
263 
264  rCalc.Pop();
265 
266  if( !rCalc.IsCalcError() )
267  m_bValidValue = true;
268  else
269  m_nValue = 0;
270 
271  return m_nValue;
272 }
273 
274 OUString SwUserFieldType::GetContent( sal_uInt32 nFormat )
275 {
276  if (nFormat && nFormat != SAL_MAX_UINT32)
277  {
278  OUString sFormattedValue;
279  const Color* pCol = nullptr;
280 
281  SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
282 
283  pFormatter->GetOutputString(GetValue(), nFormat, sFormattedValue, &pCol);
284  return sFormattedValue;
285  }
286 
287  return m_aContent;
288 }
289 
290 void SwUserFieldType::SetContent( const OUString& rStr, sal_uInt32 nFormat )
291 {
292  if( m_aContent == rStr )
293  return;
294 
295  m_aContent = rStr;
296 
297  if (nFormat && nFormat != SAL_MAX_UINT32)
298  {
299  double fValue;
300 
301  if (GetDoc()->IsNumberFormat(rStr, nFormat, fValue))
302  {
303  SetValue(fValue);
304  LanguageTag aContentLanguage(GetFieldTypeLanguage());
305  m_aContentLang = aContentLanguage.getBcp47();
306  m_aContent = DoubleToString(fValue, nFormat);
307  }
308  }
309 
310  bool bModified = GetDoc()->getIDocumentState().IsModified();
312  if( !bModified ) // Bug 57028
313  {
315  }
316 }
317 
318 void SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
319 {
320  switch( nWhichId )
321  {
322  case FIELD_PROP_DOUBLE:
323  rAny <<= m_nValue;
324  break;
325  case FIELD_PROP_PAR2:
326  rAny <<= m_aContent;
327  break;
328  case FIELD_PROP_BOOL1:
329  rAny <<= 0 != (nsSwGetSetExpType::GSE_EXPR&m_nType);
330  break;
331  default:
332  assert(false);
333  }
334 }
335 
336 void SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
337 {
338  switch( nWhichId )
339  {
340  case FIELD_PROP_DOUBLE:
341  {
342  double fVal = 0;
343  rAny >>= fVal;
344  m_nValue = fVal;
345  LanguageTag aContentLanguage(GetFieldTypeLanguage());
346  m_aContentLang = aContentLanguage.getBcp47();
347  m_aContent = DoubleToString(m_nValue, static_cast<sal_uInt16>(GetFieldTypeLanguage()));
348  }
349  break;
350  case FIELD_PROP_PAR2:
351  rAny >>= m_aContent;
352  break;
353  case FIELD_PROP_BOOL1:
354  if(*o3tl::doAccess<bool>(rAny))
355  {
358  }
359  else
360  {
363  }
364  break;
365  default:
366  assert(false);
367  }
368 }
369 
371 {
372  (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwUserFieldType"));
373  (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("nValue"), BAD_CAST(OString::number(m_nValue).getStr()));
374  (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("aContent"), BAD_CAST(m_aContent.toUtf8().getStr()));
375  (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("aContentLang"), BAD_CAST(m_aContentLang.toUtf8().getStr()));
376  SwFieldType::dumpAsXml(pWriter);
377  (void)xmlTextWriterEndElement(pWriter);
378 }
379 
380 /* 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:191
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: usrfld.cxx:59
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: usrfld.cxx:198
The shared part of a user field.
Definition: usrfld.hxx:34
virtual double GetValue() const override
Definition: usrfld.cxx:81
virtual OUString GetPar2() const override
Get content.
Definition: usrfld.cxx:98
virtual OUString GetName() const override
Only in derived classes.
Definition: usrfld.cxx:211
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:1412
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:246
bool m_bDeleted
Definition: usrfld.hxx:37
const OUString & getBcp47(bool bResolveSystem=true) const
Definition: doc.hxx:187
const LanguageTag & getLanguageTag() const
CharClass * GetCharClass()
Definition: calc.cxx:628
bool m_bValidValue
Definition: usrfld.hxx:36
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:144
static const OUString & GetTypeStr(SwFieldTypesEnum nTypeId)
Definition: fldbas.cxx:121
The root element of a Writer document layout.
Definition: rootfrm.hxx:82
OUString Expand(sal_uInt32 nFormat, sal_uInt16 nSubType, LanguageType nLng)
Definition: usrfld.cxx:186
#define FIELD_PROP_FORMAT
Definition: unofldmid.h:26
void UpdateFields() const
Definition: fldbas.cxx:148
#define SAL_MAX_UINT32
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:357
void Pop()
Definition: calc.cxx:621
double m_nValue
Float value type.
Definition: usrfld.hxx:39
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId)
Definition: fldbas.cxx:357
#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:344
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const override
Definition: usrfld.cxx:119
virtual OUString GetFieldName() const override
get name or content
Definition: usrfld.cxx:74
struct _xmlTextWriter * xmlTextWriterPtr
double GetDouble() const
Definition: calc.cxx:1450
virtual std::unique_ptr< SwField > Copy() const override
Definition: usrfld.cxx:67
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:103
virtual void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: fldbas.cxx:159
SwFieldIds
Definition: fldbas.hxx:44
OUString m_aName
Definition: usrfld.hxx:40
void SetCalcError(SwCalcError eErr)
Definition: calc.hxx:245
void UnlockModify()
Definition: calbck.hxx:204
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:203
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: usrfld.cxx:370
SwDoc * GetDoc() const
Definition: fldbas.hxx:419
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:379
virtual void SetValue(const double &rVal) override
Definition: usrfld.cxx:86
SwFieldType * GetTyp() const
Definition: fldbas.hxx:392
virtual void SwClientNotify(const SwModify &, const SfxHint &) override
Definition: usrfld.cxx:216
virtual OUString GetPar1() const override
Get name.
Definition: usrfld.cxx:92
SwUserField(SwUserFieldType *, sal_uInt16 nSub, sal_uInt32 nFormat)
Definition: usrfld.cxx:53
#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:358
bool IsModifyLocked() const
Definition: calbck.hxx:205
virtual sal_uInt16 GetSubType() const override
Definition: usrfld.cxx:108
OUString ExpandValue(const double &rVal, sal_uInt32 nFormat, LanguageType nLng) const
return value formatted as string
Definition: fldbas.cxx:526
virtual OUString GetName() const
Only in derived classes.
Definition: fldbas.cxx:136
#define FIELD_PROP_PAR2
Definition: unofldmid.h:24
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: usrfld.cxx:167
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: fldbas.cxx:673
SwUserFieldType(SwDoc *pDocPtr, const OUString &)
Definition: usrfld.cxx:175
void SetValue(const double nVal)
Definition: usrfld.hxx:84
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId) override
Definition: usrfld.cxx:138
virtual void SetSubType(sal_uInt16 nSub) override
Definition: usrfld.cxx:113
OUString GetContent(sal_uInt32 nFormat=0)
Definition: usrfld.cxx:274
struct _ADOUser User
virtual void CallSwClientNotify(const SfxHint &rHint) const override
Definition: calbck.cxx:324
void setLanguageTag(const LanguageTag &rLanguageTag)
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:318
OUString DoubleToString(const double &rVal, LanguageType eLng) const
Definition: fldbas.cxx:587
bool Push(const SwUserFieldType *pUserFieldType)
Definition: calc.cxx:612
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:290
virtual void PutValue(const css::uno::Any &rVal, sal_uInt16 nMId) override
Definition: usrfld.cxx:336
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo
sal_uInt16 m_nSubType
Definition: usrfld.hxx:104