merge the formfield patch from ooo-build
[ooovba.git] / reportdesign / source / ui / misc / FunctionHelper.cxx
blobae53861c4775fc3ac11b03b10d6b941e2f6995f5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: formula.hxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include "precompiled_reportdesign.hxx"
33 #include "FunctionHelper.hxx"
34 #include <tools/debug.hxx>
36 // =============================================================================
37 namespace rptui
39 // =============================================================================
40 using namespace ::com::sun::star;
42 FunctionManager::FunctionManager(const uno::Reference< report::meta::XFunctionManager>& _xMgr)
43 : m_xMgr(_xMgr)
46 FunctionManager::~FunctionManager()
49 sal_Unicode FunctionManager::getSingleToken(const formula::IFunctionManager::EToken _eToken) const
51 switch(_eToken)
53 case eOk:
54 return sal_Unicode('(');
55 case eClose:
56 return sal_Unicode(')');
57 case eSep:
58 return sal_Unicode(';');
59 case eArrayOpen:
60 return sal_Unicode('{');
61 case eArrayClose:
62 return sal_Unicode('}');
63 } // switch(_eToken)
64 return 0;
66 // -----------------------------------------------------------------------------
67 sal_uInt32 FunctionManager::getCount() const
69 return m_xMgr->getCount();
71 // -----------------------------------------------------------------------------
72 const formula::IFunctionCategory* FunctionManager::getCategory(sal_uInt32 _nPos) const
74 if ( _nPos >= m_aCategoryIndex.size() )
76 uno::Reference< report::meta::XFunctionCategory> xCategory = m_xMgr->getCategory(_nPos);
77 ::boost::shared_ptr< FunctionCategory > pCategory(new FunctionCategory(this,_nPos + 1,xCategory));
78 m_aCategoryIndex.push_back( m_aCategories.insert(TCategoriesMap::value_type(xCategory->getName(),pCategory)).first );
80 return m_aCategoryIndex[_nPos]->second.get();
82 // -----------------------------------------------------------------------------
83 const formula::IFunctionDescription* FunctionManager::getFunctionByName(const ::rtl::OUString& _sFunctionName) const
85 const formula::IFunctionDescription* pDesc = NULL;
86 try
88 pDesc = get(m_xMgr->getFunctionByName(_sFunctionName)).get();
90 catch(uno::Exception&)
93 return pDesc;
95 // -----------------------------------------------------------------------------
96 void FunctionManager::fillLastRecentlyUsedFunctions(::std::vector< const formula::IFunctionDescription*>& /*_rLastRUFunctions*/) const
98 //const sal_uInt32 nCount = getCount();
99 //for(sal_uInt32 i = 0 ; i < nCount ; ++i)
101 // const formula::IFunctionCategory* pCategory = getCategory(
104 // -----------------------------------------------------------------------------
105 ::boost::shared_ptr< FunctionDescription > FunctionManager::get(const uno::Reference< report::meta::XFunctionDescription>& _xFunctionDescription) const
107 ::boost::shared_ptr< FunctionDescription > pDesc;
108 if ( _xFunctionDescription.is() )
110 const ::rtl::OUString sFunctionName = _xFunctionDescription->getName();
111 TFunctionsMap::const_iterator aFunctionFind = m_aFunctions.find(sFunctionName);
112 if ( aFunctionFind == m_aFunctions.end() )
114 const uno::Reference< report::meta::XFunctionCategory> xCategory = _xFunctionDescription->getCategory();
115 const ::rtl::OUString sCategoryName = xCategory->getName();
116 TCategoriesMap::iterator aCategoryFind = m_aCategories.find(sCategoryName);
117 if ( aCategoryFind == m_aCategories.end() )
119 aCategoryFind = m_aCategories.insert(TCategoriesMap::value_type(sCategoryName,::boost::shared_ptr< FunctionCategory > (new FunctionCategory(this,xCategory->getNumber() + 1,xCategory)))).first;
120 m_aCategoryIndex.push_back( aCategoryFind );
122 aFunctionFind = m_aFunctions.insert(TFunctionsMap::value_type(sFunctionName,::boost::shared_ptr<FunctionDescription>(new FunctionDescription(aCategoryFind->second.get(),_xFunctionDescription)))).first;
123 } // if ( aFind == m_aFunctions.end() )
124 pDesc = aFunctionFind->second;
125 } // if ( _xFunctionDescription.is() )
126 return pDesc;
128 // -----------------------------------------------------------------------------
129 FunctionCategory::FunctionCategory(const FunctionManager* _pFMgr,sal_uInt32 _nPos,const uno::Reference< report::meta::XFunctionCategory>& _xCategory)
130 : m_xCategory(_xCategory)
131 ,m_nFunctionCount(_xCategory->getCount())
132 , m_nNumber(_nPos)
133 ,m_pFunctionManager(_pFMgr)
136 // -----------------------------------------------------------------------------
137 sal_uInt32 FunctionCategory::getCount() const
139 return m_nFunctionCount;
141 // -----------------------------------------------------------------------------
142 const formula::IFunctionDescription* FunctionCategory::getFunction(sal_uInt32 _nPos) const
144 if ( _nPos >= m_aFunctions.size() && _nPos < m_nFunctionCount )
146 uno::Reference< report::meta::XFunctionDescription> xFunctionDescription = m_xCategory->getFunction(_nPos);
147 ::boost::shared_ptr< FunctionDescription > pFunction = m_pFunctionManager->get(xFunctionDescription);
148 m_aFunctions.push_back( pFunction );
150 return m_aFunctions[_nPos].get();
152 // -----------------------------------------------------------------------------
153 sal_uInt32 FunctionCategory::getNumber() const
155 return m_nNumber;
157 // -----------------------------------------------------------------------------
158 const formula::IFunctionManager* FunctionCategory::getFunctionManager() const
160 return m_pFunctionManager;
162 // -----------------------------------------------------------------------------
163 ::rtl::OUString FunctionCategory::getName() const
165 return m_xCategory->getName();
167 // -----------------------------------------------------------------------------
168 FunctionDescription::FunctionDescription(const formula::IFunctionCategory* _pFunctionCategory,const uno::Reference< report::meta::XFunctionDescription>& _xFunctionDescription)
169 : m_xFunctionDescription(_xFunctionDescription)
170 , m_pFunctionCategory(_pFunctionCategory)
172 m_aParameter = m_xFunctionDescription->getArguments();
174 ::rtl::OUString FunctionDescription::getFunctionName() const
176 return m_xFunctionDescription->getName();
178 // -----------------------------------------------------------------------------
179 const formula::IFunctionCategory* FunctionDescription::getCategory() const
181 return m_pFunctionCategory;
183 // -----------------------------------------------------------------------------
184 ::rtl::OUString FunctionDescription::getDescription() const
186 return m_xFunctionDescription->getDescription();
188 // -----------------------------------------------------------------------------
189 xub_StrLen FunctionDescription::getSuppressedArgumentCount() const
191 return static_cast<xub_StrLen>(m_aParameter.getLength());
193 // -----------------------------------------------------------------------------
194 ::rtl::OUString FunctionDescription::getFormula(const ::std::vector< ::rtl::OUString >& _aArguments) const
196 ::rtl::OUString sFormula;
199 const ::rtl::OUString *pArguments = _aArguments.empty() ? 0 : &_aArguments[0];
200 sFormula = m_xFunctionDescription->createFormula(uno::Sequence< ::rtl::OUString >(pArguments, _aArguments.size()));
202 catch(const uno::Exception&)
204 DBG_ERROR("Exception caught!");
206 return sFormula;
208 // -----------------------------------------------------------------------------
209 void FunctionDescription::fillVisibleArgumentMapping(::std::vector<USHORT>& _rArguments) const
211 const sal_Int32 nCount = m_aParameter.getLength();
212 for(USHORT i = 0;i < nCount; ++i)
214 _rArguments.push_back(i);
217 // -----------------------------------------------------------------------------
218 void FunctionDescription::initArgumentInfo() const
221 // -----------------------------------------------------------------------------
222 ::rtl::OUString FunctionDescription::getSignature() const
224 return m_xFunctionDescription->getSignature();
226 // -----------------------------------------------------------------------------
227 long FunctionDescription::getHelpId() const
229 return 0;
231 // -----------------------------------------------------------------------------
232 sal_uInt32 FunctionDescription::getParameterCount() const
234 return m_aParameter.getLength();
236 // -----------------------------------------------------------------------------
237 ::rtl::OUString FunctionDescription::getParameterName(sal_uInt32 _nPos) const
239 if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) )
240 return m_aParameter[_nPos].Name;
241 return ::rtl::OUString();
243 // -----------------------------------------------------------------------------
244 ::rtl::OUString FunctionDescription::getParameterDescription(sal_uInt32 _nPos) const
246 if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) )
247 return m_aParameter[_nPos].Description;
248 return ::rtl::OUString();
250 // -----------------------------------------------------------------------------
251 bool FunctionDescription::isParameterOptional(sal_uInt32 _nPos) const
253 if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) )
254 return m_aParameter[_nPos].IsOptional;
255 return false;
257 // -----------------------------------------------------------------------------
258 // =============================================================================
259 } // rptui
260 // =============================================================================