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