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::GetContent( sal_uInt32 nFormat )
279{
280 if (nFormat && nFormat != SAL_MAX_UINT32)
281 {
282 OUString sFormattedValue;
283 const Color* pCol = nullptr;
284
286
287 pFormatter->GetOutputString(GetValue(), nFormat, sFormattedValue, &pCol);
288 return sFormattedValue;
289 }
290
291 return m_aContent;
292}
293
294void SwUserFieldType::SetContent( const OUString& rStr, sal_uInt32 nFormat )
295{
296 if( m_aContent == rStr )
297 return;
298
299 m_aContent = rStr;
300
301 if (nFormat && nFormat != SAL_MAX_UINT32)
302 {
303 double fValue;
304
305 if (GetDoc()->IsNumberFormat(rStr, nFormat, fValue))
306 {
307 SetValue(fValue);
308 LanguageTag aContentLanguage(GetFieldTypeLanguage());
309 m_aContentLang = aContentLanguage.getBcp47();
310 m_aContent = DoubleToString(fValue, nFormat);
311 }
312 }
313
314 bool bModified = GetDoc()->getIDocumentState().IsModified();
316 if( !bModified ) // Bug 57028
317 {
319 }
320}
321
322void SwUserFieldType::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
323{
324 switch( nWhichId )
325 {
327 rAny <<= m_nValue;
328 break;
329 case FIELD_PROP_PAR2:
330 rAny <<= m_aContent;
331 break;
332 case FIELD_PROP_BOOL1:
333 rAny <<= 0 != (nsSwGetSetExpType::GSE_EXPR&m_nType);
334 break;
335 default:
336 assert(false);
337 }
338}
339
340void SwUserFieldType::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
341{
342 switch( nWhichId )
343 {
345 {
346 double fVal = 0;
347 rAny >>= fVal;
348 m_nValue = fVal;
349 LanguageTag aContentLanguage(GetFieldTypeLanguage());
350 m_aContentLang = aContentLanguage.getBcp47();
351 m_aContent = DoubleToString(m_nValue, static_cast<sal_uInt16>(GetFieldTypeLanguage()));
352 }
353 break;
354 case FIELD_PROP_PAR2:
355 rAny >>= m_aContent;
356 break;
357 case FIELD_PROP_BOOL1:
358 if(*o3tl::doAccess<bool>(rAny))
359 {
362 }
363 else
364 {
367 }
368 break;
369 default:
370 assert(false);
371 }
372}
373
375{
376 (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwUserFieldType"));
377 (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("nValue"), BAD_CAST(OString::number(m_nValue).getStr()));
378 (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("aContent"), BAD_CAST(m_aContent.toUtf8().getStr()));
379 (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("aContentLang"), BAD_CAST(m_aContentLang.toUtf8().getStr()));
380 SwFieldType::dumpAsXml(pWriter);
381 (void)xmlTextWriterEndElement(pWriter);
382}
383
384/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOUser User
struct _xmlTextWriter * xmlTextWriterPtr
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?
virtual void SetUndoNoResetModified()=0
Disable (re)setting the document modified flag on Undo/Redo.
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:195
void SetCalcError(SwCalcError eErr)
Definition: calc.hxx:250
void Pop()
Definition: calc.cxx:624
const CharClass * GetCharClass() const
Definition: calc.cxx:631
void SetCharClass(const LanguageTag &rLanguageTag)
Definition: calc.cxx:636
SwSbxValue Calculate(const OUString &rStr)
Definition: calc.cxx:362
bool Push(const SwUserFieldType *pUserFieldType)
Definition: calc.cxx:615
bool IsCalcError() const
Definition: calc.hxx:251
Definition: doc.hxx:192
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:395
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:145
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:358
SvNumberFormatter * GetNumberFormatter(bool bCreate=true)
Definition: doc.hxx:1414
virtual void UpdateFields()
Definition: fldbas.cxx:213
virtual OUString GetName() const
Only in derived classes.
Definition: fldbas.cxx:137
static const OUString & GetTypeStr(SwFieldTypesEnum nTypeId)
Definition: fldbas.cxx:122
virtual void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: fldbas.cxx:155
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:383
void SetFormat(sal_uInt32 const nSet)
Definition: fldbas.hxx:307
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:403
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const
Definition: fldbas.cxx:351
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId)
Definition: fldbas.cxx:369
SwFieldType * GetTyp() const
Definition: fldbas.hxx:398
LanguageType GetLanguage() const
Language at field position.
Definition: fldbas.hxx:408
const OUString & GetTitle() const
Definition: fldbas.hxx:394
void LockModify()
Definition: calbck.hxx:208
bool IsModifyLocked() const
Definition: calbck.hxx:210
void UnlockModify()
Definition: calbck.hxx:209
The root element of a Writer document layout.
Definition: rootfrm.hxx:82
double GetDouble() const
Definition: calc.cxx:1467
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:84
virtual void QueryValue(css::uno::Any &rVal, sal_uInt16 nMId) const override
Definition: usrfld.cxx:322
bool m_bDeleted
Definition: usrfld.hxx:37
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: usrfld.cxx:374
OUString m_aContent
String value type.
Definition: usrfld.hxx:42
void SetContent(const OUString &rStr, sal_uInt32 nFormat=0)
Definition: usrfld.cxx:294
virtual void PutValue(const css::uno::Any &rVal, sal_uInt16 nMId) override
Definition: usrfld.cxx:340
double GetValue() const
Definition: usrfld.hxx:81
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: usrfld.cxx:199
OUString GetContent(sal_uInt32 nFormat=0)
Definition: usrfld.cxx:278
sal_uInt16 m_nType
Definition: usrfld.hxx:45
double m_nValue
Float value type.
Definition: usrfld.hxx:39
virtual void SwClientNotify(const SwModify &, const SfxHint &) override
Definition: usrfld.cxx:217
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:104
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:415
OUString ExpandValue(const double &rVal, sal_uInt32 nFormat, LanguageType nLng) const
return value formatted as string
Definition: fldbas.cxx:547
OUString DoubleToString(const double &rVal, LanguageType eLng) const
Definition: fldbas.cxx:608
SwDoc * GetDoc() const
Definition: fldbas.hxx:425
void EnableFormat(bool bFormat=true)
Definition: fldbas.hxx:435
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: fldbas.cxx:694
virtual void CallSwClientNotify(const SfxHint &rHint) const override
Definition: calbck.cxx:325
virtual OUString GetName() const override
SwFieldIds
Definition: fldbas.hxx:45
#define LANGUAGE_SYSTEM
const SwExtendedSubType SUB_CMD
Show command.
Definition: fldbas.hxx:212
const SwExtendedSubType SUB_INVISIBLE
Invisible.
Definition: fldbas.hxx:213
const SwGetSetExpType GSE_EXPR
Expression.
Definition: fldbas.hxx:204
const SwGetSetExpType GSE_STRING
String.
Definition: fldbas.hxx:203
#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