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  xmlTextWriterStartElement(pWriter, BAD_CAST("SwUserField"));
170  xmlTextWriterWriteAttribute(pWriter, BAD_CAST("nSubType"), BAD_CAST(OString::number(m_nSubType).getStr()));
171  SwValueField::dumpAsXml(pWriter);
172  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_nType = m_nType;
203  pTmp->m_bValidValue = m_bValidValue;
204  pTmp->m_nValue = m_nValue;
205  pTmp->m_bDeleted = m_bDeleted;
206 
207  return pTmp;
208 }
209 
210 OUString SwUserFieldType::GetName() const
211 {
212  return m_aName;
213 }
214 
215 void SwUserFieldType::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
216 {
217  if( !pOld && !pNew )
218  m_bValidValue = false;
219 
220  NotifyClients( pOld, pNew );
221 
222  // update input fields that might be connected to the user field
223  if ( !IsModifyLocked() )
224  {
225  LockModify();
227  UnlockModify();
228  }
229 }
230 
232 {
233  if(m_bValidValue)
234  return m_nValue;
235 
236  if(!rCalc.Push( this ))
237  {
239  return 0;
240  }
241 
242  // See if we need to temporarily switch rCalc's language: in case it
243  // differs from the field type locale.
244  CharClass* pCharClass = rCalc.GetCharClass();
245  LanguageTag aCalcLanguage = pCharClass->getLanguageTag();
246  LanguageTag aFieldTypeLanguage(GetFieldTypeLanguage());
247  bool bSwitchLanguage = aCalcLanguage != aFieldTypeLanguage;
248  if (bSwitchLanguage)
249  pCharClass->setLanguageTag(aFieldTypeLanguage);
250 
251  m_nValue = rCalc.Calculate( m_aContent ).GetDouble();
252 
253  if (bSwitchLanguage)
254  pCharClass->setLanguageTag(aCalcLanguage);
255 
256  rCalc.Pop();
257 
258  if( !rCalc.IsCalcError() )
259  m_bValidValue = true;
260  else
261  m_nValue = 0;
262 
263  return m_nValue;
264 }
265 
266 OUString SwUserFieldType::GetContent( sal_uInt32 nFormat )
267 {
268  if (nFormat && nFormat != SAL_MAX_UINT32)
269  {
270  OUString sFormattedValue;
271  Color* pCol = nullptr;
272 
273  SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
274 
275  pFormatter->GetOutputString(GetValue(), nFormat, sFormattedValue, &pCol);
276  return sFormattedValue;
277  }
278 
279  return m_aContent;
280 }
281 
282 void SwUserFieldType::SetContent( const OUString& rStr, sal_uInt32 nFormat )
283 {
284  if( m_aContent != rStr )
285  {
286  m_aContent = rStr;
287 
288  if (nFormat && nFormat != SAL_MAX_UINT32)
289  {
290  double fValue;
291 
292  if (GetDoc()->IsNumberFormat(rStr, nFormat, fValue))
293  {
294  SetValue(fValue);
295  m_aContent = DoubleToString(fValue, nFormat);
296  }
297  }
298 
299  bool bModified = GetDoc()->getIDocumentState().IsModified();
301  if( !bModified ) // Bug 57028
302  {
304  }
305  }
306 }
307 
308 void SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
309 {
310  switch( nWhichId )
311  {
312  case FIELD_PROP_DOUBLE:
313  rAny <<= m_nValue;
314  break;
315  case FIELD_PROP_PAR2:
316  rAny <<= m_aContent;
317  break;
318  case FIELD_PROP_BOOL1:
319  rAny <<= 0 != (nsSwGetSetExpType::GSE_EXPR&m_nType);
320  break;
321  default:
322  assert(false);
323  }
324 }
325 
326 void SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
327 {
328  switch( nWhichId )
329  {
330  case FIELD_PROP_DOUBLE:
331  {
332  double fVal = 0;
333  rAny >>= fVal;
334  m_nValue = fVal;
335 
336  m_aContent = DoubleToString(m_nValue, static_cast<sal_uInt16>(GetFieldTypeLanguage()));
337  }
338  break;
339  case FIELD_PROP_PAR2:
340  rAny >>= m_aContent;
341  break;
342  case FIELD_PROP_BOOL1:
343  if(*o3tl::doAccess<bool>(rAny))
344  {
347  }
348  else
349  {
352  }
353  break;
354  default:
355  assert(false);
356  }
357 }
358 
360 {
361  xmlTextWriterStartElement(pWriter, BAD_CAST("SwUserFieldType"));
362  xmlTextWriterWriteAttribute(pWriter, BAD_CAST("nValue"), BAD_CAST(OString::number(m_nValue).getStr()));
363  xmlTextWriterWriteAttribute(pWriter, BAD_CAST("aContent"), BAD_CAST(m_aContent.toUtf8().getStr()));
364  SwFieldType::dumpAsXml(pWriter);
365  xmlTextWriterEndElement(pWriter);
366 }
367 
368 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 m_nType
Definition: usrfld.hxx:44
Definition: calc.hxx:184
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:35
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:210
LanguageType GetLanguage() const
Language at field position.
Definition: fldbas.hxx:393
void EnableFormat(bool bFormat=true)
Definition: fldbas.hxx:420
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:388
SvNumberFormatter * GetNumberFormatter(bool bCreate=true)
Definition: doc.hxx:1405
const SwExtendedSubType SUB_INVISIBLE
Invisible.
Definition: fldbas.hxx:206
void GetOutputString(const double &fOutNumber, sal_uInt32 nFIndex, OUString &sOutString, Color **ppColor, bool bUseStarFormat=false)
virtual void SetModified()=0
Must be called manually at changes of format.
bool IsCalcError() const
Definition: calc.hxx:238
bool m_bDeleted
Definition: usrfld.hxx:38
Definition: doc.hxx:185
const LanguageTag & getLanguageTag() const
CharClass * GetCharClass()
Definition: calc.cxx:610
bool m_bValidValue
Definition: usrfld.hxx:37
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:143
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
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.hxx:272
#define SAL_MAX_UINT32
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:356
void Pop()
Definition: calc.cxx:603
double m_nValue
Float value type.
Definition: usrfld.hxx:40
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId)
Definition: fldbas.cxx:306
#define FIELD_PROP_DOUBLE
Definition: unofldmid.h:34
const SwGetSetExpType GSE_EXPR
Expression.
Definition: fldbas.hxx:197
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const
Definition: fldbas.cxx:293
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:1410
virtual std::unique_ptr< SwField > Copy() const override
Definition: usrfld.cxx:67
static OUString GetTypeStr(SwFieldTypesEnum nTypeId)
Definition: fldbas.cxx:120
OUString m_aContent
String value type.
Definition: usrfld.hxx:43
const SwExtendedSubType SUB_CMD
Show command.
Definition: fldbas.hxx:205
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:147
SwFieldIds
Definition: fldbas.hxx:38
OUString m_aName
Definition: usrfld.hxx:41
void SetCalcError(SwCalcError eErr)
Definition: calc.hxx:237
void UnlockModify()
Definition: calbck.hxx:214
double GetValue() const
Definition: usrfld.hxx:80
void SetFormat(sal_uInt32 const nSet)
Definition: fldbas.hxx:294
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:393
void LockModify()
Definition: calbck.hxx:213
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: usrfld.cxx:359
SwDoc * GetDoc() const
Definition: fldbas.hxx:410
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:370
virtual void SetValue(const double &rVal) override
Definition: usrfld.cxx:86
SwFieldType * GetTyp() const
Definition: fldbas.hxx:383
virtual OUString GetPar1() const override
Get name.
Definition: usrfld.cxx:92
void NotifyClients(const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue)
Definition: calbck.cxx:167
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:399
SwSbxValue Calculate(const OUString &rStr)
Definition: calc.cxx:347
bool IsModifyLocked() const
Definition: calbck.hxx:218
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:467
virtual OUString GetName() const
Only in derived classes.
Definition: fldbas.cxx:135
#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:616
SwUserFieldType(SwDoc *pDocPtr, const OUString &)
Definition: usrfld.cxx:175
virtual void Modify(const SfxPoolItem *pOld, const SfxPoolItem *pNew) override
Definition: usrfld.cxx:215
void SetValue(const double nVal)
Definition: usrfld.hxx:83
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:266
struct _ADOUser User
void setLanguageTag(const LanguageTag &rLanguageTag)
const SwGetSetExpType GSE_STRING
String.
Definition: fldbas.hxx:196
virtual SwFieldType * GetSysFieldType(const SwFieldIds eWhich) const =0
virtual void QueryValue(css::uno::Any &rVal, sal_uInt16 nMId) const override
Definition: usrfld.cxx:308
OUString DoubleToString(const double &rVal, LanguageType eLng) const
Definition: fldbas.cxx:530
bool Push(const SwUserFieldType *pUserFieldType)
Definition: calc.cxx:594
void SetContent(const OUString &rStr, sal_uInt32 nFormat=0)
Definition: usrfld.cxx:282
virtual void PutValue(const css::uno::Any &rVal, sal_uInt16 nMId) override
Definition: usrfld.cxx:326
sal_uInt16 m_nSubType
Definition: usrfld.hxx:103