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  if (rHint.GetId() == SfxHintId::SwLegacyModify)
219  {
220  auto pLegacy = static_cast<const sw::LegacyModifyHint*>(&rHint);
221  if (!pLegacy->m_pOld && !pLegacy->m_pNew)
222  m_bValidValue = false;
223  }
224 
225  CallSwClientNotify(rHint);
226  // update input fields that might be connected to the user field
227  if (!IsModifyLocked())
228  {
229  LockModify();
231  UnlockModify();
232  }
233 }
234 
236 {
237  if(m_bValidValue)
238  return m_nValue;
239 
240  if(!rCalc.Push( this ))
241  {
243  return 0;
244  }
245 
246  // See if we need to temporarily switch rCalc's language: in case it
247  // differs from the field type locale.
248  CharClass* pCharClass = rCalc.GetCharClass();
249  LanguageTag aCharClassLanguage = pCharClass->getLanguageTag();
250  LanguageTag aContentLang(m_aContentLang);
251 
252  // for the call of calculate we need the language that was used for putting/setting
253  // the m_aContent string, otherwise the aContent could be interpreted wrongly,
254 
255  bool bSwitchLanguage = m_aContentLang != aCharClassLanguage.getBcp47();
256 
257  if (bSwitchLanguage)
258  pCharClass->setLanguageTag(aContentLang);
259 
260  m_nValue = rCalc.Calculate( m_aContent ).GetDouble();
261 
262  // we than have to set the proper char class languageTag again
263 
264  if (bSwitchLanguage)
265  pCharClass->setLanguageTag(aCharClassLanguage);
266 
267  rCalc.Pop();
268 
269  if( !rCalc.IsCalcError() )
270  m_bValidValue = true;
271  else
272  m_nValue = 0;
273 
274  return m_nValue;
275 }
276 
277 OUString SwUserFieldType::GetContent( sal_uInt32 nFormat )
278 {
279  if (nFormat && nFormat != SAL_MAX_UINT32)
280  {
281  OUString sFormattedValue;
282  const Color* pCol = nullptr;
283 
284  SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
285 
286  pFormatter->GetOutputString(GetValue(), nFormat, sFormattedValue, &pCol);
287  return sFormattedValue;
288  }
289 
290  return m_aContent;
291 }
292 
293 void SwUserFieldType::SetContent( const OUString& rStr, sal_uInt32 nFormat )
294 {
295  if( m_aContent == rStr )
296  return;
297 
298  m_aContent = rStr;
299 
300  if (nFormat && nFormat != SAL_MAX_UINT32)
301  {
302  double fValue;
303 
304  if (GetDoc()->IsNumberFormat(rStr, nFormat, fValue))
305  {
306  SetValue(fValue);
307  LanguageTag aContentLanguage(GetFieldTypeLanguage());
308  m_aContentLang = aContentLanguage.getBcp47();
309  m_aContent = DoubleToString(fValue, nFormat);
310  }
311  }
312 
313  bool bModified = GetDoc()->getIDocumentState().IsModified();
315  if( !bModified ) // Bug 57028
316  {
318  }
319 }
320 
321 void SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
322 {
323  switch( nWhichId )
324  {
325  case FIELD_PROP_DOUBLE:
326  rAny <<= m_nValue;
327  break;
328  case FIELD_PROP_PAR2:
329  rAny <<= m_aContent;
330  break;
331  case FIELD_PROP_BOOL1:
332  rAny <<= 0 != (nsSwGetSetExpType::GSE_EXPR&m_nType);
333  break;
334  default:
335  assert(false);
336  }
337 }
338 
339 void SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
340 {
341  switch( nWhichId )
342  {
343  case FIELD_PROP_DOUBLE:
344  {
345  double fVal = 0;
346  rAny >>= fVal;
347  m_nValue = fVal;
348  LanguageTag aContentLanguage(GetFieldTypeLanguage());
349  m_aContentLang = aContentLanguage.getBcp47();
350  m_aContent = DoubleToString(m_nValue, static_cast<sal_uInt16>(GetFieldTypeLanguage()));
351  }
352  break;
353  case FIELD_PROP_PAR2:
354  rAny >>= m_aContent;
355  break;
356  case FIELD_PROP_BOOL1:
357  if(*o3tl::doAccess<bool>(rAny))
358  {
361  }
362  else
363  {
366  }
367  break;
368  default:
369  assert(false);
370  }
371 }
372 
374 {
375  (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwUserFieldType"));
376  (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("nValue"), BAD_CAST(OString::number(m_nValue).getStr()));
377  (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("aContent"), BAD_CAST(m_aContent.toUtf8().getStr()));
378  (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("aContentLang"), BAD_CAST(m_aContentLang.toUtf8().getStr()));
379  SwFieldType::dumpAsXml(pWriter);
380  (void)xmlTextWriterEndElement(pWriter);
381 }
382 
383 /* 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: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: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:246
bool m_bDeleted
Definition: usrfld.hxx:37
const OUString & getBcp47(bool bResolveSystem=true) const
Definition: doc.hxx:188
const LanguageTag & getLanguageTag() const
CharClass * GetCharClass()
Definition: calc.cxx:624
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
SfxHintId GetId() const
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:617
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:119
virtual OUString GetFieldName() const override
get name or content
Definition: usrfld.cxx:74
struct _xmlTextWriter * xmlTextWriterPtr
double GetDouble() const
Definition: calc.cxx:1446
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:373
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:354
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:528
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:675
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:277
struct _ADOUser User
virtual void CallSwClientNotify(const SfxHint &rHint) const override
Definition: calbck.cxx:326
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:321
OUString DoubleToString(const double &rVal, LanguageType eLng) const
Definition: fldbas.cxx:589
bool Push(const SwUserFieldType *pUserFieldType)
Definition: calc.cxx:608
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:293
virtual void PutValue(const css::uno::Any &rVal, sal_uInt16 nMId) override
Definition: usrfld.cxx:339
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo
sal_uInt16 m_nSubType
Definition: usrfld.hxx:104