1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sfx2/dispatch.hxx>
21 #include <sfx2/docfile.hxx>
22 #include <sfx2/viewfrm.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/mnemonic.hxx>
25 #include <vcl/tabpage.hxx>
26 #include <vcl/tabctrl.hxx>
27 #include <vcl/lstbox.hxx>
28 #include <vcl/group.hxx>
29 #include <vcl/wall.hxx>
31 #include <svtools/stdctrl.hxx>
32 #include <svtools/svmedit.hxx>
33 #include <svtools/treelistbox.hxx>
34 #include <svl/stritem.hxx>
35 #include <svl/zforlist.hxx>
36 #include <svl/eitem.hxx>
38 #include <unotools/charclass.hxx>
39 #include <tools/diagnose_ex.h>
41 #include "formdlgs.hrc"
42 #include "funcpage.hxx"
43 #include "formula/formula.hxx"
44 #include "formula/IFunctionDescription.hxx"
45 #include "formula/FormulaCompiler.hxx"
46 #include "formula/token.hxx"
47 #include "formula/tokenarray.hxx"
48 #include "formula/formdata.hxx"
49 #include "formula/formulahelper.hxx"
50 #include "structpg.hxx"
51 #include "parawin.hxx"
52 #include "ModuleHelper.hxx"
53 #include "ForResId.hrc"
54 #include <com/sun/star/sheet/FormulaToken.hpp>
55 #include <com/sun/star/sheet/FormulaLanguage.hpp>
56 #include <com/sun/star/sheet/FormulaMapGroup.hpp>
57 #include <com/sun/star/sheet/FormulaMapGroupSpecialOffset.hpp>
58 #include <com/sun/star/beans/XPropertySet.hpp>
59 #include <boost/ref.hpp>
60 #include <boost/shared_ptr.hpp>
61 #include <comphelper/processfactory.hxx>
62 #include <comphelper/string.hxx>
70 using namespace ::com::sun::star
;
72 class OFormulaToken
: public IFormulaToken
74 sal_Int32 m_nParaCount
;
78 OFormulaToken(bool _bFunction
,sal_Int32 _nParaCount
) : m_nParaCount(_nParaCount
),m_bIsFunction(_bFunction
){}
80 virtual ~OFormulaToken() {}
82 virtual bool isFunction() const { return m_bIsFunction
; }
83 virtual sal_uInt32
getArgumentCount() const { return m_nParaCount
; }
90 ::std::pair
<RefButton
*,RefEdit
*>
91 RefInputStartBefore( RefEdit
* pEdit
, RefButton
* pButton
);
92 void RefInputStartAfter( RefEdit
* pEdit
, RefButton
* pButton
);
93 void RefInputDoneAfter( sal_Bool bForced
);
94 sal_Bool
CalcValue( const String
& rStrExp
, String
& rStrResult
);
95 sal_Bool
CalcStruct( const String
& rStrExp
);
98 xub_StrLen
GetFunctionPos(xub_StrLen nPos
);
101 void MakeTree(IStructHelper
* _pTree
,SvTreeListEntry
* pParent
,FormulaToken
* _pToken
,long Count
);
102 void fillTree(IStructHelper
* _pTree
);
103 void UpdateTokenArray( const String
& rStrExp
);
104 String
RepairFormula(const String
& aFormula
);
105 void FillDialog(sal_Bool nFlag
=sal_True
);
106 void EditNextFunc( sal_Bool bForward
, xub_StrLen nFStart
=NOT_FOUND
);
107 void EditThisFunc(xub_StrLen nFStart
);
109 void StoreFormEditData(FormEditData
* pEditData
);
111 void UpdateArgInput( sal_uInt16 nOffset
, sal_uInt16 nInput
);
113 void Update(const String
& _sExp
);
115 void SaveArg( sal_uInt16 nEd
);
116 void UpdateSelection();
117 void DoEnter( sal_Bool bOk
);
118 void FillListboxes();
119 void FillControls(sal_Bool
&rbNext
, sal_Bool
&rbPrev
);
121 FormulaDlgMode
SetMeText(const String
& _sText
,xub_StrLen PrivStart
, xub_StrLen PrivEnd
,sal_Bool bMatrix
,sal_Bool _bSelect
,sal_Bool _bUpdate
);
122 void SetMeText(const String
& _sText
);
123 sal_Bool
CheckMatrix(String
& aFormula
/*IN/OUT*/);
125 void SetEdSelection();
127 sal_Bool
UpdateParaWin(Selection
& _rSelection
);
128 void UpdateParaWin(const Selection
& _rSelection
,const String
& _sRefStr
);
130 void SetData(xub_StrLen nFStart
,xub_StrLen nNextFStart
,xub_StrLen nNextFEnd
,xub_StrLen
& PrivStart
,xub_StrLen
& PrivEnd
);
131 void PreNotify( NotifyEvent
& rNEvt
);
133 RefEdit
* GetCurrRefEdit();
135 const FormulaHelper
& GetFormulaHelper() const;
136 uno::Reference
< sheet::XFormulaOpCodeMapper
> GetFormulaOpCodeMapper() const;
138 DECL_LINK( ModifyHdl
, ParaWin
* );
139 DECL_LINK( FxHdl
, ParaWin
* );
141 DECL_LINK(MatrixHdl
, void *);
142 DECL_LINK(FormulaHdl
, void *);
143 DECL_LINK(FormulaCursorHdl
, void *);
144 DECL_LINK( BtnHdl
, PushButton
* );
145 DECL_LINK( GetEdFocusHdl
, ArgInput
* );
146 DECL_LINK( GetFxFocusHdl
, ArgInput
* );
147 DECL_LINK(DblClkHdl
, void *);
148 DECL_LINK(FuncSelHdl
, void *);
149 DECL_LINK(StructSelHdl
, void *);
151 mutable uno::Reference
< sheet::XFormulaOpCodeMapper
> m_xOpCodeMapper
;
152 uno::Sequence
< sheet::FormulaToken
> m_aTokenList
;
153 ::std::auto_ptr
<FormulaTokenArray
> m_pTokenArray
;
154 mutable uno::Sequence
< sheet::FormulaOpCodeMapEntry
> m_aSpecialOpCodes
;
155 mutable const sheet::FormulaOpCodeMapEntry
* m_pSpecialOpCodesEnd
;
156 mutable uno::Sequence
< sheet::FormulaToken
> m_aSeparatorsOpCodes
;
157 mutable uno::Sequence
< sheet::FormulaOpCodeMapEntry
> m_aFunctionOpCodes
;
158 mutable const sheet::FormulaOpCodeMapEntry
* m_pFunctionOpCodesEnd
;
159 mutable uno::Sequence
< sheet::FormulaOpCodeMapEntry
> m_aUnaryOpCodes
;
160 mutable const sheet::FormulaOpCodeMapEntry
* m_pUnaryOpCodesEnd
;
161 mutable uno::Sequence
< sheet::FormulaOpCodeMapEntry
> m_aBinaryOpCodes
;
162 mutable const sheet::FormulaOpCodeMapEntry
* m_pBinaryOpCodesEnd
;
163 ::std::vector
< ::boost::shared_ptr
<OFormulaToken
> > m_aTokens
;
164 ::std::map
<FormulaToken
*,sheet::FormulaToken
> m_aTokenMap
;
165 IFormulaEditorHelper
* m_pHelper
;
167 IControlReferenceHandler
* m_pDlg
;
169 GroupBox aGEdit
; //! MUST be placed before pParaWin for initializing
171 FixedText aFtHeadLine
;
172 FixedInfo aFtFuncName
;
173 FixedInfo aFtFuncDesc
;
175 FixedText aFtEditName
;
180 FixedText aFtFormula
;
185 CancelButton aBtnCancel
;
187 PushButton aBtnBackward
;
188 PushButton aBtnForward
;
194 FixedText aFtFormResult
;
195 ValWnd aWndFormResult
;
197 RefEdit
* pTheRefEdit
;
198 RefButton
* pTheRefButton
;
200 StructPage
* pStructPage
;
202 sal_Bool bStructUpdate
;
203 MultiLineEdit
* pMEdit
;
204 sal_Bool bUserMatrixFlag
;
207 const String aTitle1
;
208 const String aTitle2
;
209 const String aTxtEnd
;
210 const String aTxtOk
; // behind aBtnEnd
214 OString m_aEditHelpId
;
219 sal_Bool bIsShutDown
;
225 const IFunctionDescription
* pFuncDesc
;
227 ::std::vector
< OUString
> m_aArguments
;
230 FormulaDlg_Impl(Dialog
* pParent
231 , bool _bSupportFunctionResult
232 , bool _bSupportResult
233 , bool _bSupportMatrix
234 ,IFormulaEditorHelper
* _pHelper
235 ,const IFunctionManager
* _pFunctionMgr
236 ,IControlReferenceHandler
* _pDlg
);
240 FormulaDlg_Impl::FormulaDlg_Impl(Dialog
* pParent
241 , bool _bSupportFunctionResult
242 , bool _bSupportResult
243 , bool _bSupportMatrix
244 ,IFormulaEditorHelper
* _pHelper
245 ,const IFunctionManager
* _pFunctionMgr
246 ,IControlReferenceHandler
* _pDlg
)
248 m_pHelper (_pHelper
),
251 aTabCtrl ( pParent
, ModuleRes( TC_FUNCTION
) ),
252 aGEdit ( pParent
, ModuleRes( GB_EDIT
) ),
253 aFtHeadLine ( pParent
, ModuleRes( FT_HEADLINE
) ),
254 aFtFuncName ( pParent
, ModuleRes( FT_FUNCNAME
) ),
255 aFtFuncDesc ( pParent
, ModuleRes( FT_FUNCDESC
) ),
256 aFtEditName ( pParent
, ModuleRes( FT_EDITNAME
) ),
257 aFtResult ( pParent
, ModuleRes( FT_RESULT
) ),
258 aWndResult ( pParent
, ModuleRes( WND_RESULT
) ),
260 aFtFormula ( pParent
, ModuleRes( FT_FORMULA
) ),
261 aMEFormula ( pParent
, ModuleRes( ED_FORMULA
) ),
262 aBtnMatrix ( pParent
, ModuleRes( BTN_MATRIX
) ),
263 aBtnHelp ( pParent
, ModuleRes( BTN_HELP
) ),
264 aBtnCancel ( pParent
, ModuleRes( BTN_CANCEL
) ),
265 aBtnBackward ( pParent
, ModuleRes( BTN_BACKWARD
) ),
266 aBtnForward ( pParent
, ModuleRes( BTN_FORWARD
) ),
267 aBtnEnd ( pParent
, ModuleRes( BTN_END
) ),
268 aEdRef ( pParent
, _pDlg
, &aFtEditName
, ModuleRes( ED_REF
) ),
269 aRefBtn ( pParent
, ModuleRes( RB_REF
), &aEdRef
, _pDlg
),
270 aFtFormResult ( pParent
, ModuleRes( FT_FORMULA_RESULT
)),
271 aWndFormResult ( pParent
, ModuleRes( WND_FORMULA_RESULT
)),
274 bUserMatrixFlag (sal_False
),
275 aTitle1 ( ModuleRes( STR_TITLE1
) ), // local resource
276 aTitle2 ( ModuleRes( STR_TITLE2
) ), // local resource
277 aTxtEnd ( ModuleRes( STR_END
) ), // local resource
278 aTxtOk ( aBtnEnd
.GetText() ),
279 m_aFormulaHelper(_pFunctionMgr
),
280 bIsShutDown (sal_False
),
285 pParaWin
= new ParaWin( pParent
,_pDlg
, aGEdit
.GetPosPixel());
292 pMEdit
= aMEFormula
.GetEdit();
294 aMEFormula
.SetAccessibleName(aFtFormula
.GetText());
296 pMEdit
->SetAccessibleName(aFtFormula
.GetText());
298 m_aEditHelpId
= pMEdit
->GetHelpId();
299 pMEdit
->SetUniqueId( m_aEditHelpId
);
302 bStructUpdate
=sal_True
;
303 Point aPos
=aGEdit
.GetPosPixel();
304 pParaWin
->SetPosPixel(aPos
);
305 pParaWin
->SetArgModifiedHdl(LINK( this, FormulaDlg_Impl
, ModifyHdl
) );
306 pParaWin
->SetFxHdl(LINK( this, FormulaDlg_Impl
, FxHdl
) );
308 pFuncPage
= new FuncPage( &aTabCtrl
,_pFunctionMgr
);
309 pStructPage
= new StructPage( &aTabCtrl
);
312 aTabCtrl
.SetTabPage( TP_FUNCTION
, pFuncPage
);
313 aTabCtrl
.SetTabPage( TP_STRUCT
, pStructPage
);
315 aOldHelp
= pParent
->GetHelpId(); // HelpId from resource always for "Page 1"
316 aOldUnique
= pParent
->GetUniqueId();
318 aFtResult
.Show( _bSupportResult
);
319 aWndResult
.Show( _bSupportResult
);
321 aFtFormResult
.Show( _bSupportFunctionResult
);
322 aWndFormResult
.Show( _bSupportFunctionResult
);
324 if ( _bSupportMatrix
)
325 aBtnMatrix
.SetClickHdl(LINK( this, FormulaDlg_Impl
, MatrixHdl
) );
329 aBtnCancel
.SetClickHdl( LINK( this, FormulaDlg_Impl
, BtnHdl
) );
330 aBtnEnd
.SetClickHdl( LINK( this, FormulaDlg_Impl
, BtnHdl
) );
331 aBtnForward
.SetClickHdl( LINK( this, FormulaDlg_Impl
, BtnHdl
) );
332 aBtnBackward
.SetClickHdl( LINK( this, FormulaDlg_Impl
, BtnHdl
) );
334 pFuncPage
->SetDoubleClickHdl( LINK( this, FormulaDlg_Impl
, DblClkHdl
) );
335 pFuncPage
->SetSelectHdl( LINK( this, FormulaDlg_Impl
, FuncSelHdl
) );
336 pStructPage
->SetSelectionHdl( LINK( this, FormulaDlg_Impl
, StructSelHdl
) );
337 pMEdit
->SetModifyHdl( LINK( this, FormulaDlg_Impl
, FormulaHdl
) );
338 aMEFormula
.SetSelChangedHdl( LINK( this, FormulaDlg_Impl
, FormulaCursorHdl
) );
340 aFntLight
= aFtFormula
.GetFont();
341 aFntLight
.SetTransparent( sal_True
);
342 aFntBold
= aFntLight
;
343 aFntBold
.SetWeight( WEIGHT_BOLD
);
345 pParaWin
->SetArgumentFonts(aFntBold
,aFntLight
);
347 // function description for choosing a function is no longer in a different color
349 aFtHeadLine
.SetFont(aFntBold
);
350 aFtFuncName
.SetFont(aFntLight
);
351 aFtFuncDesc
.SetFont(aFntLight
);
353 FormulaDlg_Impl::~FormulaDlg_Impl()
355 if(aTimer
.IsActive())
357 aTimer
.SetTimeoutHdl(Link());
359 }// if(aTimer.IsActive())
360 bIsShutDown
=sal_True
;// Set it in order to PreNotify not to save GetFocus.
362 aTabCtrl
.RemovePage(TP_FUNCTION
);
363 aTabCtrl
.RemovePage(TP_STRUCT
);
371 void FormulaDlg_Impl::StoreFormEditData(FormEditData
* pData
)
373 if (pData
) // it won't be destroyed over Close;
375 pData
->SetFStart((xub_StrLen
)pMEdit
->GetSelection().Min());
376 pData
->SetSelection(pMEdit
->GetSelection());
378 if(aTabCtrl
.GetCurPageId()==TP_FUNCTION
)
379 pData
->SetMode( (sal_uInt16
) FORMULA_FORMDLG_FORMULA
);
381 pData
->SetMode( (sal_uInt16
) FORMULA_FORMDLG_EDIT
);
382 pData
->SetUndoStr(pMEdit
->GetText());
383 pData
->SetMatrixFlag(aBtnMatrix
.IsChecked());
387 // -----------------------------------------------------------------------------
388 void FormulaDlg_Impl::PreNotify( NotifyEvent
& rNEvt
)
390 sal_uInt16 nSwitch
=rNEvt
.GetType();
391 if(nSwitch
==EVENT_GETFOCUS
&& !bIsShutDown
)
393 Window
* pWin
=rNEvt
.GetWindow();
396 aActivWinId
= pWin
->GetUniqueId();
397 if(aActivWinId
.isEmpty())
399 Window
* pParent
=pWin
->GetParent();
402 aActivWinId
=pParent
->GetUniqueId();
404 if(!aActivWinId
.isEmpty()) break;
406 pParent
=pParent
->GetParent();
409 if(!aActivWinId
.isEmpty())
412 FormEditData
* pData
= m_pHelper
->getFormEditData();
414 if (pData
&& !aTimer
.IsActive()) // it won't be destroyed over Close;
416 pData
->SetUniqueId(aActivWinId
);
422 uno::Reference
< sheet::XFormulaOpCodeMapper
> FormulaDlg_Impl::GetFormulaOpCodeMapper() const
424 if ( !m_xOpCodeMapper
.is() )
426 m_xOpCodeMapper
= m_pHelper
->getFormulaOpCodeMapper();
427 m_aFunctionOpCodes
= m_xOpCodeMapper
->getAvailableMappings(sheet::FormulaLanguage::ODFF
,sheet::FormulaMapGroup::FUNCTIONS
);
428 m_pFunctionOpCodesEnd
= m_aFunctionOpCodes
.getConstArray() + m_aFunctionOpCodes
.getLength();
430 m_aUnaryOpCodes
= m_xOpCodeMapper
->getAvailableMappings(sheet::FormulaLanguage::ODFF
,sheet::FormulaMapGroup::UNARY_OPERATORS
);
431 m_pUnaryOpCodesEnd
= m_aUnaryOpCodes
.getConstArray() + m_aUnaryOpCodes
.getLength();
433 m_aBinaryOpCodes
= m_xOpCodeMapper
->getAvailableMappings(sheet::FormulaLanguage::ODFF
,sheet::FormulaMapGroup::BINARY_OPERATORS
);
434 m_pBinaryOpCodesEnd
= m_aBinaryOpCodes
.getConstArray() + m_aBinaryOpCodes
.getLength();
436 uno::Sequence
< OUString
> aArgs(3);
437 aArgs
[TOKEN_OPEN
] = OUString("(");
438 aArgs
[TOKEN_CLOSE
] = OUString(")");
439 aArgs
[TOKEN_SEP
] = OUString(";");
440 m_aSeparatorsOpCodes
= m_xOpCodeMapper
->getMappings(aArgs
,sheet::FormulaLanguage::ODFF
);
442 m_aSpecialOpCodes
= m_xOpCodeMapper
->getAvailableMappings(sheet::FormulaLanguage::ODFF
,sheet::FormulaMapGroup::SPECIAL
);
443 m_pSpecialOpCodesEnd
= m_aSpecialOpCodes
.getConstArray() + m_aSpecialOpCodes
.getLength();
444 } // if ( !m_xOpCodeMapper.is() )
445 return m_xOpCodeMapper
;
448 void FormulaDlg_Impl::DeleteArgs()
450 ::std::vector
< OUString
>().swap(m_aArguments
);
455 // comparing two property instances
456 struct OpCodeCompare
: public ::std::binary_function
< sheet::FormulaOpCodeMapEntry
, sal_Int32
, bool >
458 bool operator() (const sheet::FormulaOpCodeMapEntry
& x
, sal_Int32 y
) const
460 return x
.Token
.OpCode
== y
;
464 // -----------------------------------------------------------------------------
465 xub_StrLen
FormulaDlg_Impl::GetFunctionPos(xub_StrLen nPos
)
467 const sal_Unicode sep
= m_pHelper
->getFunctionManager()->getSingleToken(IFunctionManager::eSep
);
469 xub_StrLen nFuncPos
=STRING_NOTFOUND
; //@ Testwise
470 sal_Bool bFlag
=sal_False
;
471 String aFormString
= m_aFormulaHelper
.GetCharClass()->uppercase(pMEdit
->GetText());
473 if ( m_aTokenList
.getLength() )
475 const uno::Reference
< sheet::XFormulaParser
> xParser(m_pHelper
->getFormulaParser());
476 const table::CellAddress
aRefPos(m_pHelper
->getReferencePosition());
478 const sheet::FormulaToken
* pIter
= m_aTokenList
.getConstArray();
479 const sheet::FormulaToken
* pEnd
= pIter
+ m_aTokenList
.getLength();
482 xub_StrLen nTokPos
=1;
483 xub_StrLen nOldTokPos
=1;
484 xub_StrLen nPrevFuncPos
= 1;
485 short nBracketCount
= 0;
486 while ( pIter
!= pEnd
)
488 const sal_Int32 eOp
= pIter
->OpCode
;
489 uno::Sequence
<sheet::FormulaToken
> aArgs(1);
491 const String aString
= xParser
->printFormula(aArgs
, aRefPos
);
492 const sheet::FormulaToken
* pNextToken
= pIter
+ 1;
494 if(!bUserMatrixFlag
&& FormulaCompiler::IsMatrixFunction((OpCode
)eOp
) )
499 if ( eOp
== m_aSpecialOpCodes
[sheet::FormulaMapGroupSpecialOffset::PUSH
].Token
.OpCode
||
500 eOp
== m_aSpecialOpCodes
[sheet::FormulaMapGroupSpecialOffset::SPACES
].Token
.OpCode
)
502 const xub_StrLen n1
=aFormString
.Search(sep
, nTokPos
);
503 const xub_StrLen n2
=aFormString
.Search(')',nTokPos
);
504 xub_StrLen nXXX
=nTokPos
;
513 if ( pNextToken
!= pEnd
)
515 aArgs
[0] = *pNextToken
;
516 const String a2String
= xParser
->printFormula(aArgs
, aRefPos
);
517 const xub_StrLen n3
= aFormString
.Search(a2String
,nXXX
);
524 nTokPos
= sal::static_int_cast
<xub_StrLen
>( nTokPos
+ aString
.Len() );
527 if ( eOp
== m_aSeparatorsOpCodes
[TOKEN_OPEN
].OpCode
)
532 else if ( eOp
== m_aSeparatorsOpCodes
[TOKEN_CLOSE
].OpCode
)
536 nFuncPos
=nPrevFuncPos
;
538 bool bIsFunction
= ::std::find_if(m_aFunctionOpCodes
.getConstArray(),m_pFunctionOpCodesEnd
,::std::bind2nd(OpCodeCompare(),boost::cref(eOp
))) != m_pFunctionOpCodesEnd
;
540 if ( bIsFunction
&& m_aSpecialOpCodes
[sheet::FormulaMapGroupSpecialOffset::SPACES
].Token
.OpCode
!= eOp
)
542 nPrevFuncPos
= nFuncPos
;
543 nFuncPos
= nOldTokPos
;
546 if ( nOldTokPos
<= nPos
&& nPos
< nTokPos
)
550 if ( nBracketCount
< 1 )
552 nFuncPos
= pMEdit
->GetText().getLength();
556 nFuncPos
=nPrevFuncPos
;
563 nOldTokPos
= nTokPos
;
564 } // while ( pIter != pEnd )
566 catch(const uno::Exception
& )
568 OSL_FAIL("Exception caught!");
574 // -----------------------------------------------------------------------------
575 sal_Bool
FormulaDlg_Impl::CalcValue( const String
& rStrExp
, String
& rStrResult
)
577 sal_Bool bResult
= sal_True
;
579 if ( rStrExp
.Len() > 0 )
581 // Only calculate the value when there isn't any more keyboard input:
583 if ( !Application::AnyInput( VCL_INPUT_KEYBOARD
) )
585 bResult
= m_pHelper
->calculateValue(rStrExp
,rStrResult
);
594 void FormulaDlg_Impl::UpdateValues()
598 if ( CalcValue( pFuncDesc
->getFormula( m_aArguments
), aStrResult
) )
599 aWndResult
.SetValue( aStrResult
);
602 if ( CalcValue(m_pHelper
->getCurrentFormula(), aStrResult
) )
603 aWndFormResult
.SetValue( aStrResult
);
607 aWndFormResult
.SetValue( aStrResult
);
609 CalcStruct(pMEdit
->GetText());
612 sal_Bool
FormulaDlg_Impl::CalcStruct( const String
& rStrExp
)
614 sal_Bool bResult
= sal_True
;
615 xub_StrLen nLength
=rStrExp
.Len();
617 if ( rStrExp
.Len() > 0 && aOldFormula
!=rStrExp
&& bStructUpdate
)
619 // Only calculate the value when there isn't any more keyboard input:
621 if ( !Application::AnyInput( VCL_INPUT_KEYBOARD
) )
623 pStructPage
->ClearStruct();
625 String aString
=rStrExp
;
626 if(rStrExp
.GetChar(nLength
-1)=='(')
628 aString
.Erase((xub_StrLen
)(nLength
-1));
631 aString
= comphelper::string::remove(aString
, '\n');
634 if ( CalcValue(aString
, aStrResult
) )
635 aWndFormResult
.SetValue( aStrResult
);
637 UpdateTokenArray(aString
);
638 fillTree(pStructPage
);
641 if(rStrExp
.GetChar(nLength
-1)=='(')
642 UpdateTokenArray(rStrExp
);
650 // -----------------------------------------------------------------------------
651 void FormulaDlg_Impl::MakeTree(IStructHelper
* _pTree
,SvTreeListEntry
* pParent
,FormulaToken
* _pToken
,long Count
)
653 if( _pToken
!= NULL
&& Count
> 0 )
655 long nParas
= _pToken
->GetParamCount();
656 OpCode eOp
= _pToken
->GetOpCode();
658 // #i101512# for output, the original token is needed
659 FormulaToken
* pOrigToken
= (_pToken
->GetType() == svFAP
) ? _pToken
->GetFAPOrigToken() : _pToken
;
660 uno::Sequence
<sheet::FormulaToken
> aArgs(1);
661 ::std::map
<FormulaToken
*,sheet::FormulaToken
>::const_iterator itr
= m_aTokenMap
.find(pOrigToken
);
662 if (itr
== m_aTokenMap
.end())
665 aArgs
[0] = itr
->second
;
668 const table::CellAddress
aRefPos(m_pHelper
->getReferencePosition());
669 const String aResult
= m_pHelper
->getFormulaParser()->printFormula(aArgs
, aRefPos
);
673 SvTreeListEntry
* pEntry
;
675 String aTest
=_pTree
->GetEntryText(pParent
);
678 (eOp
==ocAdd
|| eOp
==ocMul
||
687 pEntry
=_pTree
->InsertEntry(aResult
,pParent
,STRUCT_ERROR
,0,_pToken
);
691 pEntry
=_pTree
->InsertEntry(aResult
,pParent
,STRUCT_FOLDER
,0,_pToken
);
695 MakeTree(_pTree
,pEntry
,m_pTokenArray
->PrevRPN(),nParas
);
697 m_pTokenArray
->NextRPN();
698 MakeTree(_pTree
,pParent
,m_pTokenArray
->PrevRPN(),Count
);
704 _pTree
->InsertEntry(aResult
,pParent
,STRUCT_ERROR
,0,_pToken
);
708 _pTree
->InsertEntry(aResult
,pParent
,STRUCT_END
,0,_pToken
);
711 MakeTree(_pTree
,pParent
,m_pTokenArray
->PrevRPN(),Count
);
714 catch(const uno::Exception
&)
716 DBG_UNHANDLED_EXCEPTION();
721 void FormulaDlg_Impl::fillTree(IStructHelper
* _pTree
)
723 GetFormulaOpCodeMapper();
724 FormulaToken
* pToken
= m_pTokenArray
->LastRPN();
728 MakeTree(_pTree
,NULL
,pToken
,1);
731 void FormulaDlg_Impl::UpdateTokenArray( const String
& rStrExp
)
734 m_aTokenList
.realloc(0);
737 const table::CellAddress
aRefPos(m_pHelper
->getReferencePosition());
738 m_aTokenList
= m_pHelper
->getFormulaParser()->parseFormula(rStrExp
, aRefPos
);
740 catch(const uno::Exception
&)
742 DBG_UNHANDLED_EXCEPTION();
744 GetFormulaOpCodeMapper(); // just to get it initialized
745 m_pTokenArray
= m_pHelper
->convertToTokenArray(m_aTokenList
);
746 const sal_Int32 nLen
= static_cast<sal_Int32
>(m_pTokenArray
->GetLen());
747 FormulaToken
** pTokens
= m_pTokenArray
->GetArray();
748 if ( pTokens
&& nLen
== m_aTokenList
.getLength() )
750 for (sal_Int32 nPos
=0; nPos
<nLen
; nPos
++)
752 m_aTokenMap
.insert(::std::map
<FormulaToken
*,sheet::FormulaToken
>::value_type(pTokens
[nPos
],m_aTokenList
[nPos
]));
754 } // if ( pTokens && nLen == m_aTokenList.getLength() )
756 FormulaCompiler
aCompiler(*m_pTokenArray
.get());
757 aCompiler
.SetCompileForFAP(sal_True
); // #i101512# special handling is needed
758 aCompiler
.CompileTokenArray();
761 void FormulaDlg_Impl::FillDialog(sal_Bool nFlag
)
763 sal_Bool bNext
=sal_True
, bPrev
=sal_True
;
765 FillControls(bNext
, bPrev
);
769 aBtnBackward
.Enable(bPrev
);
770 aBtnForward
.Enable(bNext
);
775 if ( CalcValue(m_pHelper
->getCurrentFormula(), aStrResult
) )
776 aWndFormResult
.SetValue( aStrResult
);
780 aWndFormResult
.SetValue( aStrResult
);
784 // -----------------------------------------------------------------------------
785 void FormulaDlg_Impl::FillListboxes()
787 // Switch between the "Pages"
788 FormEditData
* pData
= m_pHelper
->getFormEditData();
790 // 1. Page: select function
791 if ( pFuncDesc
&& pFuncDesc
->getCategory() )
793 if( pFuncPage
->GetCategory() != pFuncDesc
->getCategory()->getNumber() + 1 )
794 pFuncPage
->SetCategory(static_cast<sal_uInt16
>(pFuncDesc
->getCategory()->getNumber() + 1));
796 sal_uInt16 nPos
=pFuncPage
->GetFuncPos(pFuncDesc
);
798 pFuncPage
->SetFunction(nPos
);
802 pFuncPage
->SetCategory( pData
->GetCatSel() );
803 pFuncPage
->SetFunction( pData
->GetFuncSel() );
807 m_pHelper
->setDispatcherLock( sal_True
);// Activate Modal-Mode
811 // HelpId for 1. page is the one from the resource
812 m_pParent
->SetHelpId( aOldHelp
);
813 m_pParent
->SetUniqueId( aOldUnique
);
815 // -----------------------------------------------------------------------------
816 void FormulaDlg_Impl::FillControls(sal_Bool
&rbNext
, sal_Bool
&rbPrev
)
818 // Switch between the "Pages"
819 FormEditData
* pData
= m_pHelper
->getFormEditData();
823 // 2. Page or Edit: show selected function
825 xub_StrLen nFStart
= pData
->GetFStart();
826 OUString aFormula
= OUString(m_pHelper
->getCurrentFormula()) + " )";
827 xub_StrLen nNextFStart
= nFStart
;
828 xub_StrLen nNextFEnd
= 0;
831 const IFunctionDescription
* pOldFuncDesc
= pFuncDesc
;
832 sal_Bool bTestFlag
= sal_False
;
834 if ( m_aFormulaHelper
.GetNextFunc( aFormula
, sal_False
,
835 nNextFStart
, &nNextFEnd
, &pFuncDesc
, &m_aArguments
) )
837 bTestFlag
= (pOldFuncDesc
!= pFuncDesc
);
843 pParaWin
->SetFunctionDesc(pFuncDesc
);
844 aFtEditName
.SetText( pFuncDesc
->getFunctionName() );
847 const OString aHelpId
= pFuncDesc
->getHelpId();
848 if ( !aHelpId
.isEmpty() )
849 pMEdit
->SetHelpId(aHelpId
);
852 xub_StrLen nOldStart
, nOldEnd
;
853 m_pHelper
->getSelection( nOldStart
, nOldEnd
);
854 if ( nOldStart
!= nNextFStart
|| nOldEnd
!= nNextFEnd
)
856 m_pHelper
->setSelection( nNextFStart
, nNextFEnd
);
858 aFuncSel
.Min() = nNextFStart
;
859 aFuncSel
.Max() = nNextFEnd
;
862 pMEdit
->SetText(m_pHelper
->getCurrentFormula());
863 xub_StrLen PrivStart
, PrivEnd
;
864 m_pHelper
->getSelection( PrivStart
, PrivEnd
);
866 pMEdit
->SetSelection( Selection(PrivStart
, PrivEnd
));
868 nArgs
= pFuncDesc
->getSuppressedArgumentCount();
869 sal_uInt16 nOffset
= pData
->GetOffset();
870 nEdFocus
= pData
->GetEdFocus();
872 // Concatenate the Edit's for Focus-Control
875 pParaWin
->SetArgumentOffset(nOffset
);
877 xub_StrLen nArgPos
= m_aFormulaHelper
.GetArgStart( aFormula
, nFStart
, 0 );
878 xub_StrLen nEditPos
=(xub_StrLen
) pMEdit
->GetSelection().Min();
879 sal_Bool bFlag
=sal_False
;
881 for(sal_uInt16 i
=0;i
<nArgs
;i
++)
883 sal_Int32 nLength
= m_aArguments
[i
].getLength()+1;
884 pParaWin
->SetArgument(i
,m_aArguments
[i
]);
885 if(nArgPos
<=nEditPos
&& nEditPos
<nArgPos
+nLength
)
890 nArgPos
= sal::static_int_cast
<xub_StrLen
>( nArgPos
+ nLength
);
892 pParaWin
->UpdateParas();
896 pParaWin
->SetActiveLine(nActiv
);
903 aFtEditName
.SetText(String());
904 pMEdit
->SetHelpId( m_aEditHelpId
);
906 // Test, ob vorne/hinten noch mehr Funktionen sind
908 xub_StrLen nTempStart
= m_aFormulaHelper
.GetArgStart( aFormula
, nFStart
, 0 );
909 rbNext
= m_aFormulaHelper
.GetNextFunc( aFormula
, sal_False
, nTempStart
);
910 nTempStart
=(xub_StrLen
)pMEdit
->GetSelection().Min();
911 pData
->SetFStart(nTempStart
);
912 rbPrev
= m_aFormulaHelper
.GetNextFunc( aFormula
, sal_True
, nTempStart
);
914 // -----------------------------------------------------------------------------
916 void FormulaDlg_Impl::ClearAllParas()
920 pParaWin
->ClearAll();
921 aWndResult
.SetValue(String());
922 aFtFuncName
.SetText(String());
925 if(pFuncPage
->IsVisible())
930 aBtnForward
.Enable(sal_True
); //@new
936 String
FormulaDlg_Impl::RepairFormula(const String
& aFormula
)
938 OUString
aResult('=');
941 UpdateTokenArray(aFormula
);
943 if ( m_aTokenList
.getLength() )
945 const table::CellAddress
aRefPos(m_pHelper
->getReferencePosition());
946 const String
sFormula(m_pHelper
->getFormulaParser()->printFormula(m_aTokenList
, aRefPos
));
947 if ( !sFormula
.Len() || sFormula
.GetChar(0) != '=' )
954 catch(const uno::Exception
& )
956 OSL_FAIL("Exception caught!");
961 void FormulaDlg_Impl::DoEnter(sal_Bool bOk
)
963 // Accept input to the document or cancel
966 // remove dummy arguments
967 String aInputFormula
= m_pHelper
->getCurrentFormula();
968 String aString
= RepairFormula(pMEdit
->GetText());
969 m_pHelper
->setSelection(0, aInputFormula
.Len());
970 m_pHelper
->setCurrentFormula(aString
);
973 m_pHelper
->switchBack();
975 m_pHelper
->dispatch(bOk
,aBtnMatrix
.IsChecked());
977 m_pHelper
->deleteFormData();
980 m_pHelper
->doClose(bOk
);
982 // -----------------------------------------------------------------------------
984 IMPL_LINK( FormulaDlg_Impl
, BtnHdl
, PushButton
*, pBtn
)
986 if ( pBtn
== &aBtnCancel
)
988 DoEnter(sal_False
); // closes the Dialog
990 else if ( pBtn
== &aBtnEnd
)
992 DoEnter(sal_True
); // closes the Dialog
994 else if ( pBtn
== &aBtnForward
)
996 const IFunctionDescription
* pDesc
=pFuncPage
->GetFuncDesc( pFuncPage
->GetFunction() );
998 if(pDesc
==pFuncDesc
|| !pFuncPage
->IsVisible())
999 EditNextFunc( sal_True
);
1002 DblClkHdl(pFuncPage
); //new
1003 aBtnForward
.Enable(sal_False
); //new
1006 else if ( pBtn
== &aBtnBackward
)
1008 bEditFlag
=sal_False
;
1009 aBtnForward
.Enable(sal_True
);
1010 EditNextFunc( sal_False
);
1011 aMEFormula
.Invalidate();
1012 aMEFormula
.Update();
1018 // -----------------------------------------------------------------------------
1021 // --------------------------------------------------------------------------
1022 // Functions for 1. Page
1023 // --------------------------------------------------------------------------
1025 // -----------------------------------------------------------------------------
1027 // Handler for Listboxes
1029 IMPL_LINK_NOARG(FormulaDlg_Impl
, DblClkHdl
)
1031 sal_uInt16 nFunc
= pFuncPage
->GetFunction();
1034 const IFunctionDescription
* pDesc
= pFuncPage
->GetFuncDesc(nFunc
);
1035 m_pHelper
->insertEntryToLRUList(pDesc
);
1037 OUString aFuncName
= OUString( pFuncPage
->GetSelFunctionName() ) + "()";
1038 m_pHelper
->setCurrentFormula(aFuncName
);
1039 pMEdit
->ReplaceSelected(aFuncName
);
1041 Selection aSel
=pMEdit
->GetSelection();
1042 aSel
.Max()=aSel
.Max()-1;
1043 pMEdit
->SetSelection(aSel
);
1047 aSel
.Min()=aSel
.Max();
1048 pMEdit
->SetSelection(aSel
);
1052 BtnHdl(&aBtnBackward
);
1055 pParaWin
->SetEdFocus(0);
1056 aBtnForward
.Enable(sal_False
); //@New
1060 // -----------------------------------------------------------------------------
1062 // --------------------------------------------------------------------------
1063 // Functions for right Page
1064 // --------------------------------------------------------------------------
1065 void FormulaDlg_Impl::SetData(xub_StrLen nFStart
,xub_StrLen nNextFStart
,xub_StrLen nNextFEnd
,xub_StrLen
& PrivStart
,xub_StrLen
& PrivEnd
)
1069 // Notice and set new selection
1070 m_pHelper
->getSelection( nFStart
, nFEnd
);
1071 m_pHelper
->setSelection( nNextFStart
, nNextFEnd
);
1073 pMEdit
->SetText(m_pHelper
->getCurrentFormula());
1076 m_pHelper
->getSelection( PrivStart
, PrivEnd
);
1079 pMEdit
->SetSelection( Selection(PrivStart
, PrivEnd
));
1080 aMEFormula
.UpdateOldSel();
1083 FormEditData
* pData
= m_pHelper
->getFormEditData();
1084 pData
->SetFStart( nNextFStart
);
1085 pData
->SetOffset( 0 );
1086 pData
->SetEdFocus( 0 );
1090 // -----------------------------------------------------------------------------
1091 void FormulaDlg_Impl::EditThisFunc(xub_StrLen nFStart
)
1093 FormEditData
* pData
= m_pHelper
->getFormEditData();
1096 String aFormula
= m_pHelper
->getCurrentFormula();
1098 if(nFStart
==NOT_FOUND
)
1100 nFStart
= pData
->GetFStart();
1104 pData
->SetFStart(nFStart
);
1107 xub_StrLen nNextFStart
= nFStart
;
1108 xub_StrLen nNextFEnd
= 0;
1112 bFound
= m_aFormulaHelper
.GetNextFunc( aFormula
, sal_False
, nNextFStart
, &nNextFEnd
);
1115 xub_StrLen PrivStart
, PrivEnd
;
1116 SetData(nFStart
,nNextFStart
,nNextFEnd
,PrivStart
, PrivEnd
);
1117 m_pHelper
->showReference(aFormula
.Copy(PrivStart
, PrivEnd
-PrivStart
));
1125 void FormulaDlg_Impl::EditNextFunc( sal_Bool bForward
, xub_StrLen nFStart
)
1127 FormEditData
* pData
= m_pHelper
->getFormEditData();
1131 String aFormula
= m_pHelper
->getCurrentFormula();
1133 if(nFStart
==NOT_FOUND
)
1135 nFStart
= pData
->GetFStart();
1139 pData
->SetFStart(nFStart
);
1142 xub_StrLen nNextFStart
= 0;
1143 xub_StrLen nNextFEnd
= 0;
1148 nNextFStart
= m_aFormulaHelper
.GetArgStart( aFormula
, nFStart
, 0 );
1149 bFound
= m_aFormulaHelper
.GetNextFunc( aFormula
, sal_False
, nNextFStart
, &nNextFEnd
);
1153 nNextFStart
= nFStart
;
1154 bFound
= m_aFormulaHelper
.GetNextFunc( aFormula
, sal_True
, nNextFStart
, &nNextFEnd
);
1159 xub_StrLen PrivStart
, PrivEnd
;
1160 SetData(nFStart
,nNextFStart
,nNextFEnd
,PrivStart
, PrivEnd
);
1164 void FormulaDlg_Impl::SaveArg( sal_uInt16 nEd
)
1171 if ( m_aArguments
[i
].isEmpty() )
1172 m_aArguments
[i
] = OUString(" ");
1174 if(pParaWin
->GetArgument(nEd
).Len()!=0)
1175 m_aArguments
[nEd
] = pParaWin
->GetArgument(nEd
);
1177 sal_uInt16 nClearPos
=nEd
+1;
1178 for(i
=nEd
+1;i
<nArgs
;i
++)
1180 if(pParaWin
->GetArgument(i
).Len()!=0)
1186 for(i
=nClearPos
;i
<nArgs
;i
++)
1188 m_aArguments
[i
] = OUString();
1193 IMPL_LINK( FormulaDlg_Impl
, FxHdl
, ParaWin
*, pPtr
)
1197 aBtnForward
.Enable(sal_True
); //@ In order to be able to input another function.
1198 aTabCtrl
.SetCurPageId(TP_FUNCTION
);
1200 String aUndoStr
= m_pHelper
->getCurrentFormula(); // it will be added before a ";"
1201 FormEditData
* pData
= m_pHelper
->getFormEditData();
1202 if (!pData
) return 0;
1204 sal_uInt16 nArgNo
= pParaWin
->GetActiveLine();
1210 xub_StrLen nFormulaStrPos
= pData
->GetFStart();
1212 String aFormula
= m_pHelper
->getCurrentFormula();
1213 xub_StrLen n1
= m_aFormulaHelper
.GetArgStart( aFormula
, nFormulaStrPos
, nEdFocus
+pData
->GetOffset() );
1215 pData
->SetEdFocus( nEdFocus
);
1216 pData
->SaveValues();
1217 pData
->SetMode( (sal_uInt16
) FORMULA_FORMDLG_FORMULA
);
1218 pData
->SetFStart( n1
);
1219 pData
->SetUndoStr( aUndoStr
);
1222 FillDialog(sal_False
);
1223 pFuncPage
->SetFocus(); //There Parawin is not visible anymore
1228 IMPL_LINK( FormulaDlg_Impl
, ModifyHdl
, ParaWin
*, pPtr
)
1232 SaveArg(pParaWin
->GetActiveLine());
1236 CalcStruct(pMEdit
->GetText());
1241 IMPL_LINK_NOARG(FormulaDlg_Impl
, FormulaHdl
)
1244 FormEditData
* pData
= m_pHelper
->getFormEditData();
1245 if (!pData
) return 0;
1248 String aInputFormula
=m_pHelper
->getCurrentFormula();
1249 String aString
=pMEdit
->GetText();
1251 Selection aSel
=pMEdit
->GetSelection();
1254 if(aString
.Len()==0) //in case everything was cleared
1257 pMEdit
->SetText(aString
);
1260 pMEdit
->SetSelection(aSel
);
1262 else if(aString
.GetChar(nTest
)!='=') //in case it's replaced;
1264 aString
.Insert( (sal_Unicode
)'=', 0 );
1265 pMEdit
->SetText(aString
);
1268 pMEdit
->SetSelection(aSel
);
1272 m_pHelper
->setSelection(0, aInputFormula
.Len());
1273 m_pHelper
->setCurrentFormula(aString
);
1274 m_pHelper
->setSelection((xub_StrLen
)aSel
.Min(),(xub_StrLen
)aSel
.Max());
1276 xub_StrLen nPos
=(xub_StrLen
)aSel
.Min()-1;
1280 if ( CalcValue(m_pHelper
->getCurrentFormula(), aStrResult
) )
1281 aWndFormResult
.SetValue( aStrResult
);
1285 aWndFormResult
.SetValue( aStrResult
);
1287 CalcStruct(aString
);
1289 nPos
=GetFunctionPos(nPos
);
1291 if(nPos
<aSel
.Min()-1)
1293 xub_StrLen nPos1
=aString
.Search('(',nPos
);
1294 EditNextFunc( sal_False
, nPos1
);
1301 m_pHelper
->setSelection((xub_StrLen
)aSel
.Min(),(xub_StrLen
)aSel
.Max());
1302 bEditFlag
=sal_False
;
1306 IMPL_LINK_NOARG(FormulaDlg_Impl
, FormulaCursorHdl
)
1308 FormEditData
* pData
= m_pHelper
->getFormEditData();
1309 if (!pData
) return 0;
1310 xub_StrLen nFStart
= pData
->GetFStart();
1314 String aString
=pMEdit
->GetText();
1316 Selection aSel
=pMEdit
->GetSelection();
1317 m_pHelper
->setSelection((xub_StrLen
)aSel
.Min(),(xub_StrLen
)aSel
.Max());
1322 pMEdit
->SetSelection(aSel
);
1325 if(aSel
.Min()!=aString
.Len())
1327 xub_StrLen nPos
=(xub_StrLen
)aSel
.Min();
1329 nFStart
=GetFunctionPos(nPos
- 1);
1333 xub_StrLen nPos1
=m_aFormulaHelper
.GetFunctionEnd(aString
,nFStart
);
1335 if(nPos1
>nPos
|| nPos1
==STRING_NOTFOUND
)
1337 EditThisFunc(nFStart
);
1345 if(aString
.GetChar(n
)==')')
1347 else if(aString
.GetChar(n
)=='(')
1349 if(nCount
==0) break;
1354 nFStart
=m_aFormulaHelper
.GetFunctionStart(aString
,n
,sal_True
);
1355 EditThisFunc(nFStart
);
1368 m_pHelper
->setSelection((xub_StrLen
)aSel
.Min(),(xub_StrLen
)aSel
.Max());
1370 bEditFlag
=sal_False
;
1374 void FormulaDlg_Impl::UpdateSelection()
1376 m_pHelper
->setSelection((xub_StrLen
)aFuncSel
.Min(),(xub_StrLen
)aFuncSel
.Max());
1377 m_pHelper
->setCurrentFormula( pFuncDesc
->getFormula( m_aArguments
) );
1378 pMEdit
->SetText(m_pHelper
->getCurrentFormula());
1379 xub_StrLen PrivStart
, PrivEnd
;
1380 m_pHelper
->getSelection( PrivStart
, PrivEnd
);
1381 aFuncSel
.Min()=PrivStart
;
1382 aFuncSel
.Max()=PrivEnd
;
1384 nArgs
= pFuncDesc
->getSuppressedArgumentCount();
1386 String aFormula
=pMEdit
->GetText();
1387 sal_Int32 nArgPos
=m_aFormulaHelper
.GetArgStart( aFormula
,PrivStart
,0);
1389 sal_uInt16 nPos
=pParaWin
->GetActiveLine();
1391 for(sal_uInt16 i
=0;i
<nPos
;i
++)
1393 nArgPos
+= (m_aArguments
[i
].getLength() + 1);
1395 sal_Int32 nLength
= m_aArguments
[nPos
].getLength();
1397 Selection
aSel(nArgPos
,nArgPos
+nLength
);
1398 m_pHelper
->setSelection((sal_uInt16
)nArgPos
,(sal_uInt16
)(nArgPos
+nLength
));
1399 pMEdit
->SetSelection(aSel
);
1400 aMEFormula
.UpdateOldSel();
1402 ::std::pair
<RefButton
*,RefEdit
*> FormulaDlg_Impl::RefInputStartBefore( RefEdit
* pEdit
, RefButton
* pButton
)
1405 pTheRefEdit
= pEdit
;
1406 pTheRefButton
= pButton
;
1410 aEdRef
.SetRefString( pTheRefEdit
->GetText() );
1411 aEdRef
.SetSelection( pTheRefEdit
->GetSelection() );
1412 aEdRef
.SetHelpId( pTheRefEdit
->GetHelpId() );
1413 aEdRef
.SetUniqueId( pTheRefEdit
->GetUniqueId() );
1416 aRefBtn
.Show( pButton
!= NULL
);
1418 ::std::pair
<RefButton
*,RefEdit
*> aPair
;
1419 aPair
.first
= pButton
? &aRefBtn
: NULL
;
1420 aPair
.second
= &aEdRef
;
1423 void FormulaDlg_Impl::RefInputStartAfter( RefEdit
* /*pEdit*/, RefButton
* /*pButton*/ )
1425 aRefBtn
.SetEndImage();
1429 OUString aStr
= OUString(aTitle2
) + " " + aFtEditName
.GetText() + "( ";
1431 if( pParaWin
->GetActiveLine() > 0 )
1433 aStr
+= pParaWin
->GetActiveArgName();
1434 if( pParaWin
->GetActiveLine() + 1 < nArgs
)
1438 m_pParent
->SetText( MnemonicGenerator::EraseAllMnemonicChars( aStr
) );
1441 void FormulaDlg_Impl::RefInputDoneAfter( sal_Bool bForced
)
1443 aRefBtn
.SetStartImage();
1444 if( bForced
|| !aRefBtn
.IsVisible() )
1450 pTheRefEdit
->SetRefString( aEdRef
.GetText() );
1451 pTheRefEdit
->GrabFocus();
1454 pTheRefButton
->SetStartImage();
1456 sal_uInt16 nPrivActiv
= pParaWin
->GetActiveLine();
1457 pParaWin
->SetArgument( nPrivActiv
, aEdRef
.GetText() );
1458 ModifyHdl( pParaWin
);
1461 m_pParent
->SetText( aTitle1
);
1464 RefEdit
* FormulaDlg_Impl::GetCurrRefEdit()
1466 return aEdRef
.IsVisible() ? &aEdRef
: pParaWin
->GetActiveEdit();
1468 void FormulaDlg_Impl::Update()
1470 FormEditData
* pData
= m_pHelper
->getFormEditData();
1471 const String sExpression
= pMEdit
->GetText();
1472 aOldFormula
= String();
1473 UpdateTokenArray(sExpression
);
1474 FormulaCursorHdl(&aMEFormula
);
1475 CalcStruct(sExpression
);
1476 if(pData
->GetMode() == FORMULA_FORMDLG_FORMULA
)
1477 aTabCtrl
.SetCurPageId(TP_FUNCTION
);
1479 aTabCtrl
.SetCurPageId(TP_STRUCT
);
1480 aBtnMatrix
.Check(pData
->GetMatrixFlag());
1482 void FormulaDlg_Impl::Update(const String
& _sExp
)
1488 void FormulaDlg_Impl::SetMeText(const String
& _sText
)
1490 FormEditData
* pData
= m_pHelper
->getFormEditData();
1491 pMEdit
->SetText(_sText
);
1492 pMEdit
->SetSelection( pData
->GetSelection());
1493 aMEFormula
.UpdateOldSel();
1495 FormulaDlgMode
FormulaDlg_Impl::SetMeText(const String
& _sText
,xub_StrLen PrivStart
, xub_StrLen PrivEnd
,sal_Bool bMatrix
,sal_Bool _bSelect
,sal_Bool _bUpdate
)
1497 FormulaDlgMode eMode
= FORMULA_FORMDLG_FORMULA
;
1499 pMEdit
->SetText(_sText
);
1501 if ( _bSelect
|| !bEditFlag
)
1502 pMEdit
->SetSelection( Selection(PrivStart
, PrivEnd
));
1505 aMEFormula
.UpdateOldSel();
1506 pMEdit
->Invalidate();
1507 m_pHelper
->showReference(pMEdit
->GetSelected());
1508 eMode
= FORMULA_FORMDLG_EDIT
;
1510 aBtnMatrix
.Check( bMatrix
);
1511 } // if ( _bUpdate )
1514 sal_Bool
FormulaDlg_Impl::CheckMatrix(String
& aFormula
)
1516 pMEdit
->GrabFocus();
1517 xub_StrLen nLen
= aFormula
.Len();
1518 sal_Bool bMatrix
= nLen
> 3 // Matrix-Formula
1519 && aFormula
.GetChar(0) == '{'
1520 && aFormula
.GetChar(1) == '='
1521 && aFormula
.GetChar(nLen
-1) == '}';
1524 aFormula
.Erase( 0, 1 );
1525 aFormula
.Erase( aFormula
.Len()-1, 1);
1526 aBtnMatrix
.Check( bMatrix
);
1527 aBtnMatrix
.Disable();
1530 aTabCtrl
.SetCurPageId(TP_STRUCT
);
1533 IMPL_LINK_NOARG(FormulaDlg_Impl
, StructSelHdl
)
1535 bStructUpdate
=sal_False
;
1536 if(pStructPage
->IsVisible()) aBtnForward
.Enable(sal_False
); //@New
1538 bStructUpdate
=sal_True
;
1541 IMPL_LINK_NOARG(FormulaDlg_Impl
, MatrixHdl
)
1543 bUserMatrixFlag
=sal_True
;
1547 IMPL_LINK_NOARG(FormulaDlg_Impl
, FuncSelHdl
)
1549 sal_uInt16 nCat
= pFuncPage
->GetCategory();
1550 if ( nCat
== LISTBOX_ENTRY_NOTFOUND
) nCat
= 0;
1551 sal_uInt16 nFunc
= pFuncPage
->GetFunction();
1552 if ( nFunc
== LISTBOX_ENTRY_NOTFOUND
) nFunc
= 0;
1554 if ( (pFuncPage
->GetFunctionEntryCount() > 0)
1555 && (pFuncPage
->GetFunction() != LISTBOX_ENTRY_NOTFOUND
) )
1557 const IFunctionDescription
* pDesc
=pFuncPage
->GetFuncDesc( pFuncPage
->GetFunction() );
1559 if(pDesc
!=pFuncDesc
) aBtnForward
.Enable(sal_True
); //new
1563 pDesc
->initArgumentInfo(); // full argument info is needed
1565 String aSig
= pDesc
->getSignature();
1566 aFtHeadLine
.SetText( pDesc
->getFunctionName() );
1567 aFtFuncName
.SetText( aSig
);
1568 aFtFuncDesc
.SetText( pDesc
->getDescription() );
1573 aFtHeadLine
.SetText( String() );
1574 aFtFuncName
.SetText( String() );
1575 aFtFuncDesc
.SetText( String() );
1580 void FormulaDlg_Impl::UpdateParaWin(const Selection
& _rSelection
,const String
& _sRefStr
)
1582 Selection theSel
= _rSelection
;
1583 aEdRef
.ReplaceSelected( _sRefStr
);
1584 theSel
.Max() = theSel
.Min() + _sRefStr
.Len();
1585 aEdRef
.SetSelection( theSel
);
1587 //-------------------------------------
1588 // Manual Update of the results' fields:
1589 //-------------------------------------
1590 sal_uInt16 nPrivActiv
= pParaWin
->GetActiveLine();
1591 pParaWin
->SetArgument(nPrivActiv
,aEdRef
.GetText());
1592 pParaWin
->UpdateParas();
1594 Edit
* pEd
= GetCurrRefEdit();
1596 pEd
->SetSelection( theSel
);
1598 pParaWin
->SetRefMode(sal_False
);
1600 sal_Bool
FormulaDlg_Impl::UpdateParaWin(Selection
& _rSelection
)
1602 pParaWin
->SetRefMode(sal_True
);
1605 Edit
* pEd
= GetCurrRefEdit();
1606 if(pEd
!=NULL
&& pTheRefEdit
==NULL
)
1608 _rSelection
=pEd
->GetSelection();
1609 _rSelection
.Justify();
1610 aStrEd
=pEd
->GetText();
1611 aEdRef
.SetRefString(aStrEd
);
1612 aEdRef
.SetSelection( _rSelection
);
1616 _rSelection
=aEdRef
.GetSelection();
1617 _rSelection
.Justify();
1618 aStrEd
= aEdRef
.GetText();
1620 return pTheRefEdit
== NULL
;
1623 void FormulaDlg_Impl::SetEdSelection()
1625 Edit
* pEd
= GetCurrRefEdit()/*aScParaWin.GetActiveEdit()*/;
1628 Selection theSel
= aEdRef
.GetSelection();
1629 // Edit may have the focus -> call ModifyHdl in addition
1630 // to what's happening in GetFocus
1631 pEd
->GetModifyHdl().Call(pEd
);
1633 pEd
->SetSelection(theSel
);
1636 // -----------------------------------------------------------------------------
1637 const FormulaHelper
& FormulaDlg_Impl::GetFormulaHelper() const
1639 return m_aFormulaHelper
;
1641 //============================================================================
1642 FormulaModalDialog::FormulaModalDialog( Window
* pParent
1643 , bool _bSupportFunctionResult
1644 , bool _bSupportResult
1645 , bool _bSupportMatrix
1646 , IFunctionManager
* _pFunctionMgr
1647 , IControlReferenceHandler
* _pDlg
) :
1648 ModalDialog( pParent
, ModuleRes(RID_FORMULADLG_FORMULA_MODAL
) ),
1649 m_pImpl( new FormulaDlg_Impl(this,_bSupportFunctionResult
1652 ,this,_pFunctionMgr
,_pDlg
))
1655 SetText(m_pImpl
->aTitle1
);
1657 FormulaModalDialog::~FormulaModalDialog()
1660 // -----------------------------------------------------------------------------
1661 void FormulaModalDialog::Update(const String
& _sExp
)
1663 m_pImpl
->Update(_sExp
);
1666 // -----------------------------------------------------------------------------
1667 void FormulaModalDialog::SetMeText(const String
& _sText
)
1669 m_pImpl
->SetMeText(_sText
);
1672 // -----------------------------------------------------------------------------
1673 sal_Bool
FormulaModalDialog::CheckMatrix(String
& aFormula
)
1675 return m_pImpl
->CheckMatrix(aFormula
);
1677 // -----------------------------------------------------------------------------
1678 void FormulaModalDialog::Update()
1682 ::std::pair
<RefButton
*,RefEdit
*> FormulaModalDialog::RefInputStartBefore( RefEdit
* pEdit
, RefButton
* pButton
)
1684 return m_pImpl
->RefInputStartBefore( pEdit
, pButton
);
1686 void FormulaModalDialog::RefInputStartAfter( RefEdit
* pEdit
, RefButton
* pButton
)
1688 m_pImpl
->RefInputStartAfter( pEdit
, pButton
);
1690 void FormulaModalDialog::RefInputDoneAfter( sal_Bool bForced
)
1692 m_pImpl
->RefInputDoneAfter( bForced
);
1695 void FormulaModalDialog::SetFocusWin(Window
*pWin
,const OString
& nUniqueId
)
1697 if(pWin
->GetUniqueId()==nUniqueId
)
1703 sal_uInt16 nCount
=pWin
->GetChildCount();
1705 for(sal_uInt16 i
=0;i
<nCount
;i
++)
1707 Window
* pChild
=pWin
->GetChild(i
);
1708 SetFocusWin(pChild
,nUniqueId
);
1713 long FormulaModalDialog::PreNotify( NotifyEvent
& rNEvt
)
1715 m_pImpl
->PreNotify( rNEvt
);
1717 return ModalDialog::PreNotify(rNEvt
);
1720 void FormulaModalDialog::StoreFormEditData(FormEditData
* pData
)
1722 m_pImpl
->StoreFormEditData(pData
);
1725 // --------------------------------------------------------------------------
1726 // Initialisation / General functions for Dialog
1727 // --------------------------------------------------------------------------
1728 FormulaDlg::FormulaDlg( SfxBindings
* pB
, SfxChildWindow
* pCW
,
1730 , bool _bSupportFunctionResult
1731 , bool _bSupportResult
1732 , bool _bSupportMatrix
1733 , IFunctionManager
* _pFunctionMgr
, IControlReferenceHandler
* _pDlg
) :
1734 SfxModelessDialog( pB
, pCW
, pParent
, ModuleRes(RID_FORMULADLG_FORMULA
) ),
1735 m_pImpl( new FormulaDlg_Impl(this, _bSupportFunctionResult
1738 , this, _pFunctionMgr
, _pDlg
))
1741 //undo SfxModelessDialog HelpId clear hack
1742 reverseUniqueHelpIdHack(*this);
1743 SetText(m_pImpl
->aTitle1
);
1746 FormulaDlg::~FormulaDlg()
1749 // -----------------------------------------------------------------------------
1750 void FormulaDlg::Update(const String
& _sExp
)
1752 m_pImpl
->Update(_sExp
);
1755 // -----------------------------------------------------------------------------
1756 void FormulaDlg::SetMeText(const String
& _sText
)
1758 m_pImpl
->SetMeText(_sText
);
1761 // -----------------------------------------------------------------------------
1762 FormulaDlgMode
FormulaDlg::SetMeText(const String
& _sText
,xub_StrLen PrivStart
, xub_StrLen PrivEnd
,sal_Bool bMatrix
,sal_Bool _bSelect
,sal_Bool _bUpdate
)
1764 return m_pImpl
->SetMeText(_sText
,PrivStart
, PrivEnd
,bMatrix
,_bSelect
,_bUpdate
);
1766 // -----------------------------------------------------------------------------
1767 void FormulaDlg::CheckMatrix()
1769 m_pImpl
->aBtnMatrix
.Check();
1771 // -----------------------------------------------------------------------------
1772 sal_Bool
FormulaDlg::CheckMatrix(String
& aFormula
)
1774 return m_pImpl
->CheckMatrix(aFormula
);
1776 // -----------------------------------------------------------------------------
1777 String
FormulaDlg::GetMeText() const
1779 return m_pImpl
->pMEdit
->GetText();
1781 // -----------------------------------------------------------------------------
1782 void FormulaDlg::Update()
1785 m_pImpl
->aTimer
.SetTimeout(200);
1786 m_pImpl
->aTimer
.SetTimeoutHdl(LINK( this, FormulaDlg
, UpdateFocusHdl
));
1787 m_pImpl
->aTimer
.Start();
1790 // -----------------------------------------------------------------------------
1791 sal_Bool
FormulaDlg::isUserMatrix() const
1793 return m_pImpl
->bUserMatrixFlag
;
1795 void FormulaDlg::DoEnter(sal_Bool _bOk
)
1797 m_pImpl
->DoEnter(_bOk
);
1799 ::std::pair
<RefButton
*,RefEdit
*> FormulaDlg::RefInputStartBefore( RefEdit
* pEdit
, RefButton
* pButton
)
1801 return m_pImpl
->RefInputStartBefore( pEdit
, pButton
);
1803 void FormulaDlg::RefInputStartAfter( RefEdit
* pEdit
, RefButton
* pButton
)
1805 m_pImpl
->RefInputStartAfter( pEdit
, pButton
);
1807 void FormulaDlg::RefInputDoneAfter( sal_Bool bForced
)
1809 m_pImpl
->RefInputDoneAfter( bForced
);
1812 void FormulaDlg::SetFocusWin(Window
*pWin
,const OString
& nUniqueId
)
1814 if(pWin
->GetUniqueId()==nUniqueId
)
1820 sal_uInt16 nCount
=pWin
->GetChildCount();
1822 for(sal_uInt16 i
=0;i
<nCount
;i
++)
1824 Window
* pChild
=pWin
->GetChild(i
);
1825 SetFocusWin(pChild
,nUniqueId
);
1831 long FormulaDlg::PreNotify( NotifyEvent
& rNEvt
)
1833 m_pImpl
->PreNotify( rNEvt
);
1834 return SfxModelessDialog::PreNotify(rNEvt
);
1837 void FormulaDlg::disableOk()
1839 m_pImpl
->aBtnEnd
.Disable();
1842 void FormulaDlg::StoreFormEditData(FormEditData
* pData
)
1844 m_pImpl
->StoreFormEditData(pData
);
1847 // -----------------------------------------------------------------------------
1848 const IFunctionDescription
* FormulaDlg::getCurrentFunctionDescription() const
1850 OSL_VERIFY(!m_pImpl
->pFuncDesc
|| m_pImpl
->pFuncDesc
->getSuppressedArgumentCount() == m_pImpl
->nArgs
);
1851 return m_pImpl
->pFuncDesc
;
1853 // -----------------------------------------------------------------------------
1854 void FormulaDlg::UpdateParaWin(const Selection
& _rSelection
,const String
& _sRefStr
)
1856 m_pImpl
->UpdateParaWin(_rSelection
,_sRefStr
);
1858 sal_Bool
FormulaDlg::UpdateParaWin(Selection
& _rSelection
)
1860 return m_pImpl
->UpdateParaWin(_rSelection
);
1862 // -----------------------------------------------------------------------------
1863 RefEdit
* FormulaDlg::GetActiveEdit()
1865 return m_pImpl
->pParaWin
->GetActiveEdit();
1867 // -----------------------------------------------------------------------------
1868 const FormulaHelper
& FormulaDlg::GetFormulaHelper() const
1870 return m_pImpl
->GetFormulaHelper();
1872 // -----------------------------------------------------------------------------
1873 void FormulaDlg::SetEdSelection()
1875 m_pImpl
->SetEdSelection();
1877 IMPL_LINK_NOARG(FormulaDlg
, UpdateFocusHdl
)
1879 FormEditData
* pData
= m_pImpl
->m_pHelper
->getFormEditData();
1881 if (pData
) // won't be destroyed over Close;
1883 m_pImpl
->m_pHelper
->setReferenceInput(pData
);
1884 OString
nUniqueId(pData
->GetUniqueId());
1885 SetFocusWin(this,nUniqueId
);
1890 // -----------------------------------------------------------------------------
1891 void FormEditData::SaveValues()
1893 FormEditData
* pTemp
= new FormEditData(*this);
1898 // -----------------------------------------------------------------------------
1899 void FormEditData::Reset()
1904 nCatSel
= 1; //! oder 0 (zuletzt benutzte)
1909 aUniqueId
=OString();
1914 // -----------------------------------------------------------------------------
1915 const FormEditData
& FormEditData::operator=( const FormEditData
& r
)
1917 pParent
= r
.pParent
;
1919 nFStart
= r
.nFStart
;
1920 nCatSel
= r
.nCatSel
;
1921 nFuncSel
= r
.nFuncSel
;
1922 nOffset
= r
.nOffset
;
1923 nEdFocus
= r
.nEdFocus
;
1924 aUndoStr
= r
.aUndoStr
;
1925 bMatrix
= r
.bMatrix
;
1926 aUniqueId
= r
.aUniqueId
;
1927 aSelection
= r
.aSelection
;
1930 // -----------------------------------------------------------------------------
1931 FormEditData::FormEditData()
1936 FormEditData::~FormEditData()
1941 FormEditData::FormEditData( const FormEditData
& r
)
1946 // -----------------------------------------------------------------------------
1948 // -----------------------------------------------------------------------------
1950 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */