LibreOffice Module sw (master)  1
tblcalc.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 <o3tl/any.hxx>
23 
24 #include <calbck.hxx>
25 #include <cntfrm.hxx>
26 #include <doc.hxx>
27 #include <pam.hxx>
28 #include <ndtxt.hxx>
29 #include <fmtfld.hxx>
30 #include <txtfld.hxx>
31 #include <expfld.hxx>
32 #include <docfld.hxx>
33 #include <unofldmid.h>
34 
35 using namespace ::com::sun::star;
36 
38  : SwValueFieldType( pDocPtr, SwFieldIds::Table )
39 {}
40 
41 std::unique_ptr<SwFieldType> SwTableFieldType::Copy() const
42 {
43  return std::make_unique<SwTableFieldType>(GetDoc());
44 }
45 
47 {
48  if( rCalcPara.m_rCalc.IsCalcError() ) // stop if there is already an error set
49  return;
50 
51  // create pointers from box name
52  BoxNmToPtr( rCalcPara.m_pTable );
53  OUString sFormula( MakeFormula( rCalcPara ));
54  SetValue( rCalcPara.m_rCalc.Calculate( sFormula ).GetDouble() );
55  ChgValid( !rCalcPara.IsStackOverflow() ); // is the value again valid?
56 }
57 
58 SwTableField::SwTableField( SwTableFieldType* pInitType, const OUString& rFormel,
59  sal_uInt16 nType, sal_uLong nFormat )
60  : SwValueField( pInitType, nFormat ), SwTableFormula( rFormel ),
61  m_nSubType(nType)
62 {
63  m_sExpand = "0";
64 }
65 
66 std::unique_ptr<SwField> SwTableField::Copy() const
67 {
68  std::unique_ptr<SwTableField> pTmp(new SwTableField( static_cast<SwTableFieldType*>(GetTyp()),
70  pTmp->m_sExpand = m_sExpand;
71  pTmp->SwValueField::SetValue(GetValue());
72  pTmp->SwTableFormula::operator=( *this );
73  pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
74  return std::unique_ptr<SwField>(pTmp.release());
75 }
76 
78 {
79  return GetTyp()->GetName() + " " + const_cast<SwTableField *>(this)->GetCommand();
80 }
81 
84 {
85  if( !GetTyp()->HasWriterListeners() )
86  return nullptr;
87 
89  for( SwFormatField* pFormatField = aIter.First(); pFormatField; pFormatField = aIter.Next() )
90  if( this == pFormatField->GetField() )
91  return &pFormatField->GetTextField()->GetTextNode();
92  return nullptr;
93 }
94 
96 {
97  if (EXTRNL_NAME != GetNameType())
98  {
99  SwNode const*const pNd = GetNodeOfFormula();
100  SwTableNode const*const pTableNd = pNd ? pNd->FindTableNode() : nullptr;
101  if (pTableNd)
102  {
103  PtrToBoxNm( &pTableNd->GetTable() );
104  }
105  }
106  return (EXTRNL_NAME == GetNameType())
108  : OUString();
109 }
110 
111 OUString SwTableField::ExpandImpl(SwRootFrame const*const) const
112 {
114  {
115  return const_cast<SwTableField *>(this)->GetCommand();
116  }
117 
119  {
120  // it is a string
121  return m_sExpand.copy(1, m_sExpand.getLength()-2);
122  }
123 
124  return m_sExpand;
125 }
126 
127 sal_uInt16 SwTableField::GetSubType() const
128 {
129  return m_nSubType;
130 }
131 
132 void SwTableField::SetSubType(sal_uInt16 nType)
133 {
134  m_nSubType = nType;
135 }
136 
137 void SwTableField::SetValue( const double& rVal )
138 {
140  m_sExpand = static_cast<SwValueFieldType*>(GetTyp())->ExpandValue(rVal, GetFormat(), GetLanguage());
141 }
142 
143 OUString SwTableField::GetPar2() const
144 {
146 }
147 
148 void SwTableField::SetPar2(const OUString& rStr)
149 {
150  SetFormula( rStr );
151 }
152 
153 bool SwTableField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
154 {
155  bool bRet = true;
156  switch ( nWhichId )
157  {
158  case FIELD_PROP_PAR2:
159  {
160  sal_uInt16 nOldSubType = m_nSubType;
161  SwTableField* pThis = const_cast<SwTableField*>(this);
163  rAny <<= ExpandImpl(nullptr);
164  pThis->m_nSubType = nOldSubType;
165  }
166  break;
167  case FIELD_PROP_BOOL1:
168  rAny <<= 0 != (nsSwExtendedSubType::SUB_CMD & m_nSubType);
169  break;
170  case FIELD_PROP_PAR1:
171  rAny <<= m_sExpand;
172  break;
173  case FIELD_PROP_FORMAT:
174  rAny <<= static_cast<sal_Int32>(GetFormat());
175  break;
176  default:
177  bRet = false;
178  }
179  return bRet;
180 }
181 
182 bool SwTableField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
183 {
184  bool bRet = true;
185  switch ( nWhichId )
186  {
187  case FIELD_PROP_PAR2:
188  {
189  OUString sTmp;
190  rAny >>= sTmp;
191  SetFormula( sTmp );
192  }
193  break;
194  case FIELD_PROP_BOOL1:
195  if(*o3tl::doAccess<bool>(rAny))
197  else
199  break;
200  case FIELD_PROP_PAR1:
201  {
202  OUString sTmp;
203  rAny >>= sTmp;
204  ChgExpStr( sTmp );
205  }
206  break;
207  case FIELD_PROP_FORMAT:
208  {
209  sal_Int32 nTmp = 0;
210  rAny >>= nTmp;
211  SetFormat(nTmp);
212  }
213  break;
214  default:
215  bRet = false;
216  }
217  return bRet;
218 }
219 
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_uInt16 GetSubType() const override
Definition: tblcalc.cxx:127
void CalcField(SwTableCalcPara &rCalcPara)
Definition: tblcalc.cxx:46
LanguageType GetLanguage() const
Language at field position.
Definition: fldbas.hxx:393
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:388
sal_uIntPtr sal_uLong
const SwTable * m_pTable
current table
Definition: cellfml.hxx:46
bool IsCalcError() const
Definition: calc.hxx:238
virtual void SetValue(const double &rVal) override
Definition: tblcalc.cxx:137
Definition: doc.hxx:185
TElementType * Next()
Definition: calbck.hxx:376
SwTableFieldType(SwDoc *pDocPtr)
Definition: tblcalc.cxx:37
virtual void SetSubType(sal_uInt16 nType) override
Definition: tblcalc.cxx:132
OUString MakeFormula(SwTableCalcPara &rCalcPara) const
Definition: cellfml.hxx:104
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhich) const override
Definition: tblcalc.cxx:153
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
#define FIELD_PROP_FORMAT
Definition: unofldmid.h:26
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhich) override
Definition: tblcalc.cxx:182
void BoxNmToPtr(const SwTable *pTable)
create from the external formula the internal
Definition: cellfml.cxx:567
OUString GetCommand()
Definition: tblcalc.cxx:95
OUString m_sExpand
Definition: expfld.hxx:376
NameType GetNameType() const
Definition: cellfml.hxx:131
virtual void SetPar2(const OUString &rStr) override
Definition: tblcalc.cxx:148
const SwTable & GetTable() const
Definition: node.hxx:497
Implementation in tblcalc.cxx.
Definition: expfld.hxx:367
sal_uInt16 m_nSubType
Definition: expfld.hxx:377
void SetFormula(const OUString &rNew)
Definition: cellfml.hxx:137
void PtrToBoxNm(const SwTable *pTable)
create from the internal formula (for CORE) the external formula (for UI)
Definition: cellfml.cxx:542
virtual OUString GetFieldName() const override
get name or content
Definition: tblcalc.cxx:77
double GetDouble() const
Definition: calc.cxx:1410
const SwExtendedSubType SUB_CMD
Show command.
Definition: fldbas.hxx:205
TElementType * First()
Definition: calbck.hxx:345
SwFieldIds
Definition: fldbas.hxx:38
virtual OUString GetPar2() const override
The formula.
Definition: tblcalc.cxx:143
void SetFormat(sal_uInt32 const nSet)
Definition: fldbas.hxx:294
bool IsStackOverflow() const
Definition: cellfml.hxx:52
SwDoc * GetDoc() const
Definition: fldbas.hxx:410
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:370
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: tblcalc.cxx:41
SwFieldType * GetTyp() const
Definition: fldbas.hxx:383
#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
void ChgValid(bool bNew)
Definition: cellfml.hxx:134
const OUString & GetFormula() const
Definition: cellfml.hxx:136
#define FIELD_PROP_PAR1
Definition: unofldmid.h:23
virtual OUString GetName() const
Only in derived classes.
Definition: fldbas.cxx:143
#define FIELD_PROP_PAR2
Definition: unofldmid.h:24
SwTableField(SwTableFieldType *, const OUString &rFormel, sal_uInt16 nSubType, sal_uLong nFormat)
Definition: tblcalc.cxx:58
const SwGetSetExpType GSE_FORMULA
Formula.
Definition: fldbas.hxx:199
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:351
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: tblcalc.cxx:111
const SwGetSetExpType GSE_STRING
String.
Definition: fldbas.hxx:196
virtual void SetValue(const double &rVal)
Definition: fldbas.cxx:680
virtual const SwNode * GetNodeOfFormula() const override
Search TextNode containing the field.
Definition: tblcalc.cxx:83
OUString ExpandValue(const double &rVal, sal_uInt32 nFormat, LanguageType nLng) const
Definition: fldbas.hxx:451
virtual double GetValue() const
Definition: fldbas.cxx:675
void ChgExpStr(const OUString &rStr)
Definition: expfld.hxx:395
SwCalc & m_rCalc
current Calculator
Definition: cellfml.hxx:45
virtual std::unique_ptr< SwField > Copy() const override
Definition: tblcalc.cxx:66
Base class of the Writer document model elements.
Definition: node.hxx:79