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>
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
37using namespace ::com::sun::star;
38
39namespace
40{
45LanguageType GetFieldTypeLanguage()
46{
47 return LANGUAGE_SYSTEM;
48}
49}
50
51// Userfields
52
53SwUserField::SwUserField(SwUserFieldType* pTyp, sal_uInt16 nSub, sal_uInt32 nFormat)
54 : SwValueField(pTyp, nFormat),
55 m_nSubType(nSub)
56{
57}
58
59OUString SwUserField::ExpandImpl(SwRootFrame const*const) const
60{
62 return static_cast<SwUserFieldType*>(GetTyp())->Expand(GetFormat(), m_nSubType, GetLanguage());
63
64 return OUString();
65}
66
67std::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 pTmp->SetTitle(GetTitle());
72 return pTmp;
73}
74
76{
78 " " + GetTyp()->GetName() + " = " +
79 static_cast<SwUserFieldType*>(GetTyp())->GetContent();
80}
81
83{
84 return static_cast<SwUserFieldType*>(GetTyp())->GetValue();
85}
86
87void SwUserField::SetValue( const double& rVal )
88{
89 static_cast<SwUserFieldType*>(GetTyp())->SetValue(rVal);
90}
91
93OUString SwUserField::GetPar1() const
94{
95 return static_cast<const SwUserFieldType*>(GetTyp())->GetName();
96}
97
99OUString SwUserField::GetPar2() const
100{
101 return static_cast<SwUserFieldType*>(GetTyp())->GetContent(GetFormat());
102}
103
104void SwUserField::SetPar2(const OUString& rStr)
105{
106 static_cast<SwUserFieldType*>(GetTyp())->SetContent(rStr, GetFormat());
107}
108
109sal_uInt16 SwUserField::GetSubType() const
110{
111 return static_cast<SwUserFieldType*>(GetTyp())->GetType() | m_nSubType;
112}
113
114void SwUserField::SetSubType(sal_uInt16 nSub)
115{
116 static_cast<SwUserFieldType*>(GetTyp())->SetType(nSub & 0x00ff);
117 m_nSubType = nSub & 0xff00;
118}
119
120bool SwUserField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
121{
122 switch( nWhichId )
123 {
124 case FIELD_PROP_BOOL2:
126 break;
127 case FIELD_PROP_BOOL1:
129 break;
131 rAny <<= static_cast<sal_Int32>(GetFormat());
132 break;
133 default:
134 return SwField::QueryValue(rAny, nWhichId);
135 }
136 return true;
137}
138
139bool 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;
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()));
173 (void)xmlTextWriterEndElement(pWriter);
174}
175
176SwUserFieldType::SwUserFieldType( SwDoc* pDocPtr, const OUString& aNam )
177 : SwValueFieldType( pDocPtr, SwFieldIds::User ),
178 m_nValue( 0 ),
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
187OUString 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
199std::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
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
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
278OUString SwUserFieldType::GetInputOrDateTime( sal_uInt32 nFormat ) const
279{
280 return static_cast<const SwValueFieldType*>(this)->GetInputOrDateTime( m_aContent, GetValue(), nFormat);
281}
282
283OUString SwUserFieldType::GetContent( sal_uInt32 nFormat ) const
284{
285 if (nFormat && nFormat != SAL_MAX_UINT32)
286 {
287 OUString sFormattedValue;
288 const Color* pCol = nullptr;
289
291
292 pFormatter->GetOutputString(GetValue(), nFormat, sFormattedValue, &pCol);
293 return sFormattedValue;
294 }
295
296 return m_aContent;
297}
298
299void SwUserFieldType::SetContent( const OUString& rStr, sal_uInt32 nFormat )
300{
301 if( m_aContent == rStr )
302 return;
303
304 m_aContent = rStr;
305
306 if (nFormat && nFormat != SAL_MAX_UINT32)
307 {
308 double fValue;
309
310 if (GetDoc()->IsNumberFormat(rStr, nFormat, fValue))
311 {
312 SetValue(fValue);
313 LanguageTag aContentLanguage(GetFieldTypeLanguage());
314 m_aContentLang = aContentLanguage.getBcp47();
315 m_aContent = DoubleToString(fValue, aContentLanguage.getLanguageType());
316 }
317 }
318
319 bool bModified = GetDoc()->getIDocumentState().IsModified();
321 if( !bModified ) // Bug 57028
322 {
323 GetDoc()->GetIDocumentUndoRedo().SetUndoNoResetModified();
324 }
325}
326
327void SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
328{
329 switch( nWhichId )
330 {
332 rAny <<= m_nValue;
333 break;
334 case FIELD_PROP_PAR2:
335 rAny <<= m_aContent;
336 break;
337 case FIELD_PROP_BOOL1:
338 rAny <<= 0 != (nsSwGetSetExpType::GSE_EXPR&m_nType);
339 break;
340 default:
341 assert(false);
342 }
343}
344
345void SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
346{
347 switch( nWhichId )
348 {
350 {
351 double fVal = 0;
352 rAny >>= fVal;
353 m_nValue = fVal;
354 LanguageTag aContentLanguage(GetFieldTypeLanguage());
355 m_aContentLang = aContentLanguage.getBcp47();
356 m_aContent = DoubleToString(m_nValue, aContentLanguage.getLanguageType());
357 }
358 break;
359 case FIELD_PROP_PAR2:
360 rAny >>= m_aContent;
361 break;
362 case FIELD_PROP_BOOL1:
363 if(*o3tl::doAccess<bool>(rAny))
364 {
367 }
368 else
369 {
372 }
373 break;
374 default:
375 assert(false);
376 }
377}
378
380{
381 if(IsValid())
382 return;
383 SwCalc aCalc(*GetDoc());
384 GetValue(aCalc);
385}
386
388{
389 (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwUserFieldType"));
390 (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("nValue"), BAD_CAST(OString::number(m_nValue).getStr()));
391 (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("aContent"), BAD_CAST(m_aContent.toUtf8().getStr()));
392 (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("aContentLang"), BAD_CAST(m_aContentLang.toUtf8().getStr()));
393 SwFieldType::dumpAsXml(pWriter);
394 (void)xmlTextWriterEndElement(pWriter);
395}
396
397/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOUser User
const LanguageTag & getLanguageTag() const
virtual SwFieldType * GetSysFieldType(const SwFieldIds eWhich) const =0
virtual void SetModified()=0
Must be called manually at changes of format.
virtual bool IsModified() const =0
Changes of document?
LanguageType getLanguageType(bool bResolveSystem=true) const
const OUString & getBcp47(bool bResolveSystem=true) const
SfxHintId GetId() const
void GetOutputString(const double &fOutNumber, sal_uInt32 nFIndex, OUString &sOutString, const Color **ppColor, bool bUseStarFormat=false)
Definition: calc.hxx:200
void SetCalcError(SwCalcError eErr)
Definition: calc.hxx:255
void Pop()
Definition: calc.cxx:625
const CharClass * GetCharClass() const
Definition: calc.cxx:632
void SetCharClass(const LanguageTag &rLanguageTag)
Definition: calc.cxx:637
SwSbxValue Calculate(const OUString &rStr)
Definition: calc.cxx:363
bool Push(const SwUserFieldType *pUserFieldType)
Definition: calc.cxx:616
bool IsCalcError() const
Definition: calc.hxx:256
Definition: doc.hxx:197
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:408
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:158
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:371
SvNumberFormatter * GetNumberFormatter(bool bCreate=true)
Definition: doc.hxx:1429
virtual void UpdateFields()
Definition: fldbas.cxx:219
virtual OUString GetName() const
Only in derived classes.
Definition: fldbas.cxx:139
static const OUString & GetTypeStr(SwFieldTypesEnum nTypeId)
Definition: fldbas.cxx:124
virtual void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: fldbas.cxx:157
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:387
void SetFormat(sal_uInt32 const nSet)
Definition: fldbas.hxx:311
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:407
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const
Definition: fldbas.cxx:364
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId)
Definition: fldbas.cxx:382
SwFieldType * GetTyp() const
Definition: fldbas.hxx:402
LanguageType GetLanguage() const
Language at field position.
Definition: fldbas.hxx:412
const OUString & GetTitle() const
Definition: fldbas.hxx:398
The root element of a Writer document layout.
Definition: rootfrm.hxx:85
double GetDouble() const
Definition: calc.cxx:1468
The shared part of a user field.
Definition: usrfld.hxx:35
OUString m_aContentLang
Language used by m_aContents.
Definition: usrfld.hxx:44
bool m_bValidValue
Definition: usrfld.hxx:36
OUString Expand(sal_uInt32 nFormat, sal_uInt16 nSubType, LanguageType nLng)
Definition: usrfld.cxx:187
OUString m_aName
Definition: usrfld.hxx:40
virtual OUString GetName() const override
Only in derived classes.
Definition: usrfld.cxx:212
void SetValue(const double nVal)
Definition: usrfld.hxx:87
OUString GetContent(sal_uInt32 nFormat=0) const
Definition: usrfld.cxx:283
virtual void QueryValue(css::uno::Any &rVal, sal_uInt16 nMId) const override
Definition: usrfld.cxx:327
bool m_bDeleted
Definition: usrfld.hxx:37
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: usrfld.cxx:387
OUString m_aContent
String value type.
Definition: usrfld.hxx:42
void SetContent(const OUString &rStr, sal_uInt32 nFormat=0)
Definition: usrfld.cxx:299
virtual void PutValue(const css::uno::Any &rVal, sal_uInt16 nMId) override
Definition: usrfld.cxx:345
void EnsureValid()
Definition: usrfld.cxx:379
double GetValue() const
Definition: usrfld.hxx:84
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: usrfld.cxx:199
sal_uInt16 m_nType
Definition: usrfld.hxx:45
double m_nValue
Float value type.
Definition: usrfld.hxx:39
OUString GetInputOrDateTime(sal_uInt32 nFormat) const
Definition: usrfld.cxx:278
virtual void SwClientNotify(const SwModify &, const SfxHint &) override
Definition: usrfld.cxx:217
bool IsValid() const
Definition: usrfld.hxx:81
SwUserFieldType(SwDoc *pDocPtr, const OUString &)
Definition: usrfld.cxx:176
virtual OUString GetPar2() const override
Get content.
Definition: usrfld.cxx:99
virtual sal_uInt16 GetSubType() const override
Definition: usrfld.cxx:109
virtual OUString GetPar1() const override
Get name.
Definition: usrfld.cxx:93
virtual double GetValue() const override
Definition: usrfld.cxx:82
sal_uInt16 m_nSubType
Definition: usrfld.hxx:107
virtual void SetSubType(sal_uInt16 nSub) override
Definition: usrfld.cxx:114
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const override
Definition: usrfld.cxx:120
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: usrfld.cxx:168
SwUserField(SwUserFieldType *, sal_uInt16 nSub, sal_uInt32 nFormat)
Definition: usrfld.cxx:53
virtual std::unique_ptr< SwField > Copy() const override
Definition: usrfld.cxx:67
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId) override
Definition: usrfld.cxx:139
virtual OUString GetFieldName() const override
get name or content
Definition: usrfld.cxx:75
virtual void SetValue(const double &rVal) override
Definition: usrfld.cxx:87
virtual void SetPar2(const OUString &rStr) override
Definition: usrfld.cxx:104
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: usrfld.cxx:59
Fields containing values that have to be formatted via number formatter.
Definition: fldbas.hxx:419
OUString ExpandValue(const double &rVal, sal_uInt32 nFormat, LanguageType nLng) const
return value formatted as string
Definition: fldbas.cxx:585
OUString DoubleToString(const double &rVal, LanguageType eLng) const
Definition: fldbas.cxx:646
SwDoc * GetDoc() const
Definition: fldbas.hxx:429
void EnableFormat(bool bFormat=true)
Definition: fldbas.hxx:439
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: fldbas.cxx:748
struct _xmlTextWriter * xmlTextWriterPtr
virtual OUString GetName() const override
SwFieldIds
Definition: fldbas.hxx:49
#define LANGUAGE_SYSTEM
const SwExtendedSubType SUB_CMD
Show command.
Definition: fldbas.hxx:216
const SwExtendedSubType SUB_INVISIBLE
Invisible.
Definition: fldbas.hxx:217
const SwGetSetExpType GSE_EXPR
Expression.
Definition: fldbas.hxx:208
const SwGetSetExpType GSE_STRING
String.
Definition: fldbas.hxx:207
#define SAL_MAX_UINT32
#define FIELD_PROP_BOOL1
Definition: unofldmid.h:28
#define FIELD_PROP_BOOL2
Definition: unofldmid.h:29
#define FIELD_PROP_FORMAT
Definition: unofldmid.h:26
#define FIELD_PROP_DOUBLE
Definition: unofldmid.h:34
#define FIELD_PROP_PAR2
Definition: unofldmid.h:24