19e0e4191SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39e0e4191SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49e0e4191SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59e0e4191SAndrew Rist  * distributed with this work for additional information
69e0e4191SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79e0e4191SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89e0e4191SAndrew Rist  * "License"); you may not use this file except in compliance
99e0e4191SAndrew Rist  * with the License.  You may obtain a copy of the License at
109e0e4191SAndrew Rist  *
119e0e4191SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129e0e4191SAndrew Rist  *
139e0e4191SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149e0e4191SAndrew Rist  * software distributed under the License is distributed on an
159e0e4191SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169e0e4191SAndrew Rist  * KIND, either express or implied.  See the License for the
179e0e4191SAndrew Rist  * specific language governing permissions and limitations
189e0e4191SAndrew Rist  * under the License.
199e0e4191SAndrew Rist  *
209e0e4191SAndrew Rist  *************************************************************/
219e0e4191SAndrew Rist 
229e0e4191SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25*b63233d8Sdamjan #include "precompiled_rptui.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir //----------------------------------------------------------------------------
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <vcl/svapp.hxx>
31cdf0e10cSrcweir #include <vcl/mnemonic.hxx>
32cdf0e10cSrcweir #include <vcl/msgbox.hxx>
33cdf0e10cSrcweir #include <unotools/charclass.hxx>
34cdf0e10cSrcweir #include <unotools/viewoptions.hxx>
35cdf0e10cSrcweir #include <tools/urlobj.hxx>
36cdf0e10cSrcweir #include <formula/formdata.hxx>
37cdf0e10cSrcweir #include <formula/funcutl.hxx>
38cdf0e10cSrcweir #include <formula/tokenarray.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include "Formula.hxx"
41cdf0e10cSrcweir #include "AddField.hxx"
42cdf0e10cSrcweir #include "helpids.hrc"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir //============================================================================
45cdf0e10cSrcweir namespace rptui
46cdf0e10cSrcweir {
47cdf0e10cSrcweir     using namespace formula;
48cdf0e10cSrcweir     using namespace ::com::sun::star;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir //	--------------------------------------------------------------------------
51cdf0e10cSrcweir //		Initialisierung / gemeinsame Funktionen  fuer Dialog
52cdf0e10cSrcweir //	--------------------------------------------------------------------------
53cdf0e10cSrcweir 
FormulaDialog(Window * pParent,const uno::Reference<lang::XMultiServiceFactory> & _xServiceFactory,const::boost::shared_ptr<IFunctionManager> & _pFunctionMgr,const::rtl::OUString & _sFormula,const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> & _xRowSet)54cdf0e10cSrcweir FormulaDialog::FormulaDialog(Window* pParent
55cdf0e10cSrcweir                              , const uno::Reference<lang::XMultiServiceFactory>& _xServiceFactory
56cdf0e10cSrcweir                              , const ::boost::shared_ptr< IFunctionManager >&  _pFunctionMgr
57cdf0e10cSrcweir                              , const ::rtl::OUString& _sFormula
58cdf0e10cSrcweir                              , const ::com::sun::star::uno::Reference < ::com::sun::star::beans::XPropertySet >& _xRowSet)
59cdf0e10cSrcweir     : FormulaModalDialog( pParent, false,false,false,this,_pFunctionMgr.get(),this)
60cdf0e10cSrcweir     ,m_aFunctionManager(_pFunctionMgr)
61cdf0e10cSrcweir     ,m_pFormulaData(new FormEditData())
62cdf0e10cSrcweir     ,m_pAddField(NULL)
63cdf0e10cSrcweir     ,m_xRowSet(_xRowSet)
64cdf0e10cSrcweir     ,m_pEdit(NULL)
65cdf0e10cSrcweir     ,m_sFormula(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("=")))
66cdf0e10cSrcweir     ,m_nStart(0)
67cdf0e10cSrcweir     ,m_nEnd(1)
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     if ( _sFormula.getLength() > 0 )
70cdf0e10cSrcweir 	{
71cdf0e10cSrcweir         if ( _sFormula.getStr()[0] != '=' )
72cdf0e10cSrcweir             m_sFormula += String(_sFormula);
73cdf0e10cSrcweir         else
74cdf0e10cSrcweir             m_sFormula = _sFormula;
75cdf0e10cSrcweir 	}
76cdf0e10cSrcweir     m_xParser.set(_xServiceFactory->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.report.pentaho.SOFormulaParser"))),uno::UNO_QUERY);
77cdf0e10cSrcweir     if ( m_xParser.is() )
78cdf0e10cSrcweir         m_xOpCodeMapper = m_xParser->getFormulaOpCodeMapper();
79cdf0e10cSrcweir     fill();
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
notifyChange()82cdf0e10cSrcweir void FormulaDialog::notifyChange()
83cdf0e10cSrcweir {
84cdf0e10cSrcweir }
85cdf0e10cSrcweir // -----------------------------------------------------------------------------
fill()86cdf0e10cSrcweir void FormulaDialog::fill()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     SetMeText(m_sFormula);
89cdf0e10cSrcweir     Update(m_sFormula);
90cdf0e10cSrcweir     CheckMatrix(m_sFormula);
91cdf0e10cSrcweir     Update();
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
~FormulaDialog()94cdf0e10cSrcweir FormulaDialog::~FormulaDialog()
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     if ( m_pAddField )
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         SvtViewOptions aDlgOpt( E_WINDOW, String::CreateFromAscii( HID_RPT_FIELD_SEL_WIN ) );
99cdf0e10cSrcweir         aDlgOpt.SetWindowState( ::rtl::OUString::createFromAscii( m_pAddField->GetWindowState((WINDOWSTATE_MASK_X | WINDOWSTATE_MASK_Y | WINDOWSTATE_MASK_STATE | WINDOWSTATE_MASK_MINIMIZED)).GetBuffer() ) );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         ::std::auto_ptr<Window> aTemp2(m_pAddField);
102cdf0e10cSrcweir 		m_pAddField = NULL;
103cdf0e10cSrcweir     }
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir //	--------------------------------------------------------------------------
107cdf0e10cSrcweir //							Funktionen fuer rechte Seite
108cdf0e10cSrcweir //	--------------------------------------------------------------------------
calculateValue(const String & rStrExp,String & rStrResult)109cdf0e10cSrcweir bool FormulaDialog::calculateValue( const String& rStrExp, String& rStrResult )
110cdf0e10cSrcweir {
111cdf0e10cSrcweir     rStrResult = rStrExp;
112cdf0e10cSrcweir 	return false;
113cdf0e10cSrcweir }
doClose(sal_Bool _bOk)114cdf0e10cSrcweir void FormulaDialog::doClose(sal_Bool _bOk)
115cdf0e10cSrcweir {
116cdf0e10cSrcweir     EndDialog(_bOk ? RET_OK : RET_CANCEL);
117cdf0e10cSrcweir }
insertEntryToLRUList(const IFunctionDescription *)118cdf0e10cSrcweir void FormulaDialog::insertEntryToLRUList(const IFunctionDescription*	/*_pDesc*/)
119cdf0e10cSrcweir {
120cdf0e10cSrcweir }
showReference(const String &)121cdf0e10cSrcweir void FormulaDialog::showReference(const String& /*_sFormula*/)
122cdf0e10cSrcweir {
123cdf0e10cSrcweir }
dispatch(sal_Bool,sal_Bool)124cdf0e10cSrcweir void FormulaDialog::dispatch(sal_Bool /*_bOK*/,sal_Bool /*_bMartixChecked*/)
125cdf0e10cSrcweir {
126cdf0e10cSrcweir }
setDispatcherLock(sal_Bool)127cdf0e10cSrcweir void FormulaDialog::setDispatcherLock( sal_Bool /*bLock*/ )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir }
setReferenceInput(const FormEditData *)130cdf0e10cSrcweir void FormulaDialog::setReferenceInput(const FormEditData* /*_pData*/)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir }
deleteFormData()133cdf0e10cSrcweir void FormulaDialog::deleteFormData()
134cdf0e10cSrcweir {
135cdf0e10cSrcweir }
clear()136cdf0e10cSrcweir void FormulaDialog::clear()
137cdf0e10cSrcweir {
138cdf0e10cSrcweir }
switchBack()139cdf0e10cSrcweir void FormulaDialog::switchBack()
140cdf0e10cSrcweir {
141cdf0e10cSrcweir }
getFormEditData() const142cdf0e10cSrcweir FormEditData* FormulaDialog::getFormEditData() const
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	return m_pFormulaData;
145cdf0e10cSrcweir }
setCurrentFormula(const String & _sReplacement)146cdf0e10cSrcweir void FormulaDialog::setCurrentFormula(const String& _sReplacement)
147cdf0e10cSrcweir {
148cdf0e10cSrcweir     const xub_StrLen nOldLen = m_nEnd - m_nStart;
149cdf0e10cSrcweir 	const xub_StrLen nNewLen = _sReplacement.Len();
150cdf0e10cSrcweir 	if (nOldLen)
151cdf0e10cSrcweir 		m_sFormula.Erase( m_nStart, nOldLen );
152cdf0e10cSrcweir 	if (nNewLen)
153cdf0e10cSrcweir 		m_sFormula.Insert( _sReplacement, m_nStart );
154cdf0e10cSrcweir 	m_nEnd = m_nStart + nNewLen;
155cdf0e10cSrcweir }
setSelection(xub_StrLen _nStart,xub_StrLen _nEnd)156cdf0e10cSrcweir void FormulaDialog::setSelection(xub_StrLen _nStart,xub_StrLen _nEnd)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     if ( _nStart <= _nEnd )
159cdf0e10cSrcweir 	{
160cdf0e10cSrcweir 		m_nStart = _nStart;
161cdf0e10cSrcweir 		m_nEnd = _nEnd;
162cdf0e10cSrcweir 	}
163cdf0e10cSrcweir 	else
164cdf0e10cSrcweir 	{
165cdf0e10cSrcweir 		m_nEnd = _nStart;
166cdf0e10cSrcweir 		m_nStart = _nEnd;
167cdf0e10cSrcweir 	}
168cdf0e10cSrcweir }
getSelection(xub_StrLen & _nStart,xub_StrLen & _nEnd) const169cdf0e10cSrcweir void FormulaDialog::getSelection(xub_StrLen& _nStart,xub_StrLen& _nEnd) const
170cdf0e10cSrcweir {
171cdf0e10cSrcweir     _nStart = m_nStart;
172cdf0e10cSrcweir     _nEnd = m_nEnd;
173cdf0e10cSrcweir }
getCurrentFormula() const174cdf0e10cSrcweir String FormulaDialog::getCurrentFormula() const
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     return m_sFormula;
177cdf0e10cSrcweir }
getFunctionManager()178cdf0e10cSrcweir IFunctionManager* FormulaDialog::getFunctionManager()
179cdf0e10cSrcweir {
180cdf0e10cSrcweir     return m_aFunctionManager.get();
181cdf0e10cSrcweir }
182cdf0e10cSrcweir // -----------------------------------------------------------------------------
ShowReference(const String &)183cdf0e10cSrcweir void FormulaDialog::ShowReference(const String& /*_sRef*/)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir }
186cdf0e10cSrcweir // -----------------------------------------------------------------------------
HideReference(sal_Bool)187cdf0e10cSrcweir void FormulaDialog::HideReference( sal_Bool /*bDoneRefMode*/)
188cdf0e10cSrcweir {
189cdf0e10cSrcweir }
190cdf0e10cSrcweir // -----------------------------------------------------------------------------
ReleaseFocus(RefEdit *,RefButton *)191cdf0e10cSrcweir void FormulaDialog::ReleaseFocus( RefEdit* /*pEdit*/, RefButton* /*pButton*/)
192cdf0e10cSrcweir {
193cdf0e10cSrcweir }
194cdf0e10cSrcweir // -----------------------------------------------------------------------------
ToggleCollapsed(RefEdit * _pEdit,RefButton * _pButton)195cdf0e10cSrcweir void FormulaDialog::ToggleCollapsed( RefEdit* _pEdit, RefButton* _pButton)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir     ::std::pair<RefButton*,RefEdit*> aPair = RefInputStartBefore( _pEdit, _pButton );
198cdf0e10cSrcweir     m_pEdit = aPair.second;
199cdf0e10cSrcweir     if ( m_pEdit )
200cdf0e10cSrcweir         m_pEdit->Hide();
201cdf0e10cSrcweir     if ( aPair.first )
202cdf0e10cSrcweir         aPair.first->Hide();
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     if ( !m_pAddField )
205cdf0e10cSrcweir     {
206cdf0e10cSrcweir         m_pAddField = new OAddFieldWindow(this,m_xRowSet);
207cdf0e10cSrcweir         m_pAddField->SetCreateHdl(LINK( this, FormulaDialog, OnClickHdl ) );
208cdf0e10cSrcweir         SvtViewOptions aDlgOpt( E_WINDOW, String::CreateFromAscii( HID_RPT_FIELD_SEL_WIN ) );
209cdf0e10cSrcweir         if ( aDlgOpt.Exists() )
210cdf0e10cSrcweir         {
211cdf0e10cSrcweir             m_pAddField->SetWindowState( ByteString( aDlgOpt.GetWindowState().getStr(), RTL_TEXTENCODING_ASCII_US ) );
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir         m_pAddField->Update();
216cdf0e10cSrcweir     } // if ( !m_pAddField )
217cdf0e10cSrcweir     RefInputStartAfter( aPair.second, aPair.first );
218cdf0e10cSrcweir     m_pAddField->Show();
219cdf0e10cSrcweir 
220cdf0e10cSrcweir }
221cdf0e10cSrcweir // -----------------------------------------------------------------------------
IMPL_LINK(FormulaDialog,OnClickHdl,OAddFieldWindow *,_pAddFieldDlg)222cdf0e10cSrcweir IMPL_LINK( FormulaDialog, OnClickHdl, OAddFieldWindow* ,_pAddFieldDlg)
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     const uno::Sequence< beans::PropertyValue > aArgs = _pAddFieldDlg->getSelectedFieldDescriptors();
225cdf0e10cSrcweir     // we use this way to create undo actions
226cdf0e10cSrcweir     if ( m_pEdit && aArgs.getLength() == 1)
227cdf0e10cSrcweir     {
228cdf0e10cSrcweir         uno::Sequence< beans::PropertyValue > aValue;
229cdf0e10cSrcweir         aArgs[0].Value >>= aValue;
230cdf0e10cSrcweir         ::svx::ODataAccessDescriptor aDescriptor(aValue);
231cdf0e10cSrcweir         ::rtl::OUString sName;
232cdf0e10cSrcweir         aDescriptor[ ::svx::daColumnName ] >>= sName;
233cdf0e10cSrcweir         if ( sName.getLength() )
234cdf0e10cSrcweir         {
235cdf0e10cSrcweir             sName = ::rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("[")) + sName + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("]"));
236cdf0e10cSrcweir             m_pEdit->SetText(sName);
237cdf0e10cSrcweir         }
238cdf0e10cSrcweir     } // if ( m_pEdit && aArgs.getLength() )
239cdf0e10cSrcweir     m_pEdit = NULL;
240cdf0e10cSrcweir     _pAddFieldDlg->Hide();
241cdf0e10cSrcweir     RefInputDoneAfter( sal_True );
242cdf0e10cSrcweir 
243cdf0e10cSrcweir     return 0L;
244cdf0e10cSrcweir }
245cdf0e10cSrcweir // -----------------------------------------------------------------------------
getFormulaParser() const246cdf0e10cSrcweir uno::Reference< sheet::XFormulaParser> FormulaDialog::getFormulaParser() const
247cdf0e10cSrcweir {
248cdf0e10cSrcweir     return m_xParser.get();
249cdf0e10cSrcweir }
250cdf0e10cSrcweir // -----------------------------------------------------------------------------
getFormulaOpCodeMapper() const251cdf0e10cSrcweir uno::Reference< sheet::XFormulaOpCodeMapper> FormulaDialog::getFormulaOpCodeMapper() const
252cdf0e10cSrcweir {
253cdf0e10cSrcweir     return m_xOpCodeMapper;
254cdf0e10cSrcweir }
255cdf0e10cSrcweir // -----------------------------------------------------------------------------
getReferencePosition() const256cdf0e10cSrcweir table::CellAddress FormulaDialog::getReferencePosition() const
257cdf0e10cSrcweir {
258cdf0e10cSrcweir     return table::CellAddress();
259cdf0e10cSrcweir }
260cdf0e10cSrcweir // -----------------------------------------------------------------------------
convertToTokenArray(const uno::Sequence<sheet::FormulaToken> & _aTokenList)261cdf0e10cSrcweir ::std::auto_ptr<formula::FormulaTokenArray> FormulaDialog::convertToTokenArray(const uno::Sequence< sheet::FormulaToken >& _aTokenList)
262cdf0e10cSrcweir {
263cdf0e10cSrcweir     ::std::auto_ptr<formula::FormulaTokenArray> pArray(new FormulaTokenArray());
264cdf0e10cSrcweir     pArray->Fill(_aTokenList, NULL);
265cdf0e10cSrcweir     return pArray;
266cdf0e10cSrcweir }
267cdf0e10cSrcweir // =============================================================================
268cdf0e10cSrcweir } // rptui
269cdf0e10cSrcweir // =============================================================================
270