1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_formula.hxx"
26
27 //----------------------------------------------------------------------------
28 #include <sfx2/dispatch.hxx>
29 #include <sfx2/docfile.hxx>
30 #include <sfx2/viewfrm.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/mnemonic.hxx>
33 #include <vcl/tabpage.hxx>
34 #include <vcl/tabctrl.hxx>
35 #include <vcl/lstbox.hxx>
36 #include <vcl/group.hxx>
37 #include <vcl/wall.hxx>
38
39 #include <svtools/stdctrl.hxx>
40 #include <svtools/svmedit.hxx>
41 #include <svtools/svtreebx.hxx>
42 #include <svl/stritem.hxx>
43 #include <svl/zforlist.hxx>
44 #include <svl/eitem.hxx>
45
46 #include <unotools/charclass.hxx>
47 #include <tools/urlobj.hxx>
48 #include <tools/diagnose_ex.h>
49
50 #include "formdlgs.hrc"
51 #include "funcpage.hxx"
52 #include "formula/formula.hxx"
53 #include "formula/IFunctionDescription.hxx"
54 #include "formula/FormulaCompiler.hxx"
55 #include "formula/token.hxx"
56 #include "formula/tokenarray.hxx"
57 #include "formula/formdata.hxx"
58 #include "formula/formulahelper.hxx"
59 #include "structpg.hxx"
60 #include "parawin.hxx"
61 #include "ModuleHelper.hxx"
62 #include "ForResId.hrc"
63 #include <com/sun/star/sheet/FormulaToken.hpp>
64 #include <com/sun/star/sheet/FormulaLanguage.hpp>
65 #include <com/sun/star/sheet/FormulaMapGroup.hpp>
66 #include <com/sun/star/sheet/FormulaMapGroupSpecialOffset.hpp>
67 #include <com/sun/star/beans/XPropertySet.hpp>
68 #include <boost/bind.hpp>
69 #include <comphelper/processfactory.hxx>
70 #include <map>
71 #include <functional>
72
73 #define TOKEN_OPEN 0
74 #define TOKEN_CLOSE 1
75 #define TOKEN_SEP 2
76 namespace formula
77 {
78 using namespace ::com::sun::star;
79
80 class OFormulaToken : public IFormulaToken
81 {
82 sal_Int32 m_nParaCount;
83 bool m_bIsFunction;
84
85 public:
OFormulaToken(bool _bFunction,sal_Int32 _nParaCount)86 OFormulaToken(bool _bFunction,sal_Int32 _nParaCount) : m_nParaCount(_nParaCount),m_bIsFunction(_bFunction){}
87
isFunction() const88 virtual bool isFunction() const { return m_bIsFunction; }
getArgumentCount() const89 virtual sal_uInt32 getArgumentCount() const { return m_nParaCount; }
90 };
91
92
93 class FormulaDlg_Impl
94 {
95 public:
96 ::std::pair<RefButton*,RefEdit*>
97 RefInputStartBefore( RefEdit* pEdit, RefButton* pButton );
98 void RefInputStartAfter( RefEdit* pEdit, RefButton* pButton );
99 void RefInputDoneAfter( sal_Bool bForced );
100 sal_Bool CalcValue( const String& rStrExp, String& rStrResult );
101 sal_Bool CalcStruct( const String& rStrExp);
102 void UpdateValues();
103 void DeleteArgs();
104 xub_StrLen GetFunctionPos(xub_StrLen nPos);
105 void ClearAllParas();
106
107 void MakeTree(IStructHelper* _pTree,SvLBoxEntry* pParent,FormulaToken* _pToken,long Count);
108 void fillTree(IStructHelper* _pTree);
109 void UpdateTokenArray( const String& rStrExp);
110 String RepairFormula(const String& aFormula);
111 void FillDialog(sal_Bool nFlag=sal_True);
112 void EditNextFunc( sal_Bool bForward, xub_StrLen nFStart=NOT_FOUND );
113 void EditThisFunc(xub_StrLen nFStart);
114 void EditFuncParas(xub_StrLen nEditPos);
115
116
117 void UpdateArgInput( sal_uInt16 nOffset, sal_uInt16 nInput );
118 void Update();
119 void Update(const String& _sExp);
120
121
122 void SaveArg( sal_uInt16 nEd );
123 void UpdateSelection();
124 void DoEnter( sal_Bool bOk );
125 void UpdateFunctionDesc();
126 void ResizeArgArr( const IFunctionDescription* pNewFunc );
127 void FillListboxes();
128 void FillControls(sal_Bool &rbNext, sal_Bool &rbPrev);
129
130 FormulaDlgMode SetMeText(const String& _sText,xub_StrLen PrivStart, xub_StrLen PrivEnd,sal_Bool bMatrix,sal_Bool _bSelect,sal_Bool _bUpdate);
131 void SetMeText(const String& _sText);
132 sal_Bool CheckMatrix(String& aFormula /*IN/OUT*/);
133
134 void SetEdSelection();
135
136 sal_Bool UpdateParaWin(Selection& _rSelection);
137 void UpdateParaWin(const Selection& _rSelection,const String& _sRefStr);
138
139 void SetData(xub_StrLen nFStart,xub_StrLen nNextFStart,xub_StrLen nNextFEnd,xub_StrLen& PrivStart,xub_StrLen& PrivEnd);
140 void PreNotify( NotifyEvent& rNEvt );
141
142 RefEdit* GetCurrRefEdit();
143 rtl::OString FindFocusWin(Window *pWin);
144
145 const FormulaHelper& GetFormulaHelper() const;
146 uno::Reference< sheet::XFormulaOpCodeMapper > GetFormulaOpCodeMapper() const;
147
148 DECL_LINK( ModifyHdl, ParaWin* );
149 DECL_LINK( FxHdl, ParaWin* );
150
151 DECL_LINK( MatrixHdl, CheckBox *);
152 DECL_LINK( FormulaHdl, MultiLineEdit* );
153 DECL_LINK( FormulaCursorHdl, EditBox*);
154 DECL_LINK( BtnHdl, PushButton* );
155 DECL_LINK( GetEdFocusHdl, ArgInput* );
156 DECL_LINK( GetFxFocusHdl, ArgInput* );
157 DECL_LINK( DblClkHdl, FuncPage* );
158 DECL_LINK( FuncSelHdl, FuncPage*);
159 DECL_LINK( StructSelHdl, StructPage * );
160 public:
161 OModuleClient m_aModuleClient;
162 mutable uno::Reference< sheet::XFormulaOpCodeMapper> m_xOpCodeMapper;
163 uno::Sequence< sheet::FormulaToken > m_aTokenList;
164 ::std::unique_ptr<FormulaTokenArray> m_pTokenArray;
165 mutable uno::Sequence< sheet::FormulaOpCodeMapEntry > m_aSpecialOpCodes;
166 mutable const sheet::FormulaOpCodeMapEntry* m_pSpecialOpCodesEnd;
167 mutable uno::Sequence< sheet::FormulaToken > m_aSeparatorsOpCodes;
168 mutable uno::Sequence< sheet::FormulaOpCodeMapEntry > m_aFunctionOpCodes;
169 mutable const sheet::FormulaOpCodeMapEntry* m_pFunctionOpCodesEnd;
170 mutable uno::Sequence< sheet::FormulaOpCodeMapEntry > m_aUnaryOpCodes;
171 mutable const sheet::FormulaOpCodeMapEntry* m_pUnaryOpCodesEnd;
172 mutable uno::Sequence< sheet::FormulaOpCodeMapEntry > m_aBinaryOpCodes;
173 mutable const sheet::FormulaOpCodeMapEntry* m_pBinaryOpCodesEnd;
174 ::std::vector< ::boost::shared_ptr<OFormulaToken> > m_aTokens;
175 ::std::map<FormulaToken*,sheet::FormulaToken> m_aTokenMap;
176 IFormulaEditorHelper* m_pHelper;
177 Dialog* m_pParent;
178 IControlReferenceHandler* m_pDlg;
179 TabControl aTabCtrl;
180 GroupBox aGEdit; //! MUST be placed before pParaWin for initializing
181 ParaWin* pParaWin;
182 FixedText aFtHeadLine;
183 FixedInfo aFtFuncName;
184 FixedInfo aFtFuncDesc;
185
186 FixedText aFtEditName;
187 //FixedInfo aFtEditDesc;
188
189 FixedText aFtResult;
190 ValWnd aWndResult;
191
192 FixedText aFtFormula;
193 EditBox aMEFormula;
194
195 CheckBox aBtnMatrix;
196 HelpButton aBtnHelp;
197 CancelButton aBtnCancel;
198
199 PushButton aBtnBackward;
200 PushButton aBtnForward;
201 OKButton aBtnEnd;
202
203 RefEdit aEdRef;
204 RefButton aRefBtn;
205
206 FixedText aFtFormResult;
207 ValWnd aWndFormResult;
208
209 RefEdit* pTheRefEdit;
210 RefButton* pTheRefButton;
211 FuncPage* pFuncPage;
212 StructPage* pStructPage;
213 String aOldFormula;
214 sal_Bool bStructUpdate;
215 MultiLineEdit* pMEdit;
216 sal_Bool bUserMatrixFlag;
217 Timer aTimer;
218
219 const String aTitle1;
220 const String aTitle2;
221 const String aTxtEnd;
222 const String aTxtOk; // hinter aBtnEnd
223 FormulaHelper
224 m_aFormulaHelper;
225
226 rtl::OString m_aEditHelpId;
227
228 rtl::OString aOldHelp;
229 rtl::OString aOldUnique;
230 rtl::OString aActivWinId;
231 sal_Bool bIsShutDown;
232
233
234
235 Font aFntBold;
236 Font aFntLight;
237 sal_uInt16 nEdFocus;
238 // Selection theCurSel;
239 sal_Bool bEditFlag;
240 const IFunctionDescription* pFuncDesc;
241 xub_StrLen nArgs;
242 ::std::vector< ::rtl::OUString > m_aArguments;
243 Selection aFuncSel;
244
245 FormulaDlg_Impl(Dialog* pParent
246 , bool _bSupportFunctionResult
247 , bool _bSupportResult
248 , bool _bSupportMatrix
249 ,IFormulaEditorHelper* _pHelper
250 ,const IFunctionManager* _pFunctionMgr
251 ,IControlReferenceHandler* _pDlg);
252 ~FormulaDlg_Impl();
253
254 };
FormulaDlg_Impl(Dialog * pParent,bool _bSupportFunctionResult,bool _bSupportResult,bool _bSupportMatrix,IFormulaEditorHelper * _pHelper,const IFunctionManager * _pFunctionMgr,IControlReferenceHandler * _pDlg)255 FormulaDlg_Impl::FormulaDlg_Impl(Dialog* pParent
256 , bool _bSupportFunctionResult
257 , bool _bSupportResult
258 , bool _bSupportMatrix
259 ,IFormulaEditorHelper* _pHelper
260 ,const IFunctionManager* _pFunctionMgr
261 ,IControlReferenceHandler* _pDlg)
262 :
263 m_pHelper (_pHelper),
264 m_pParent (pParent),
265 m_pDlg (_pDlg),
266 aTabCtrl ( pParent, ModuleRes( TC_FUNCTION ) ),
267 aGEdit ( pParent, ModuleRes( GB_EDIT ) ),
268 aFtHeadLine ( pParent, ModuleRes( FT_HEADLINE ) ),
269 aFtFuncName ( pParent, ModuleRes( FT_FUNCNAME ) ),
270 aFtFuncDesc ( pParent, ModuleRes( FT_FUNCDESC ) ),
271 //
272 aFtEditName ( pParent, ModuleRes( FT_EDITNAME ) ),
273 aFtResult ( pParent, ModuleRes( FT_RESULT ) ),
274 aWndResult ( pParent, ModuleRes( WND_RESULT ) ),
275
276 aFtFormula ( pParent, ModuleRes( FT_FORMULA ) ),
277 aMEFormula ( pParent, ModuleRes( ED_FORMULA ) ),
278 //
279 aBtnMatrix ( pParent, ModuleRes( BTN_MATRIX ) ),
280 aBtnHelp ( pParent, ModuleRes( BTN_HELP ) ),
281 aBtnCancel ( pParent, ModuleRes( BTN_CANCEL ) ),
282 aBtnBackward ( pParent, ModuleRes( BTN_BACKWARD ) ),
283 aBtnForward ( pParent, ModuleRes( BTN_FORWARD ) ),
284 aBtnEnd ( pParent, ModuleRes( BTN_END ) ),
285 aEdRef ( pParent, _pDlg, ModuleRes( ED_REF) ),
286 aRefBtn ( pParent, ModuleRes( RB_REF),&aEdRef,_pDlg ),
287 aFtFormResult ( pParent, ModuleRes( FT_FORMULA_RESULT)),
288 aWndFormResult ( pParent, ModuleRes( WND_FORMULA_RESULT)),
289 //
290 pTheRefEdit (NULL),
291 pMEdit (NULL),
292 bUserMatrixFlag (sal_False),
293 //
294 aTitle1 ( ModuleRes( STR_TITLE1 ) ), // lokale Resource
295 aTitle2 ( ModuleRes( STR_TITLE2 ) ), // lokale Resource
296 aTxtEnd ( ModuleRes( STR_END ) ), // lokale Resource
297 aTxtOk ( aBtnEnd.GetText() ),
298 m_aFormulaHelper(_pFunctionMgr),
299 //
300 bIsShutDown (sal_False),
301 nEdFocus (0),
302 pFuncDesc (NULL),
303 nArgs (0)
304 {
305 pParaWin = new ParaWin( pParent,_pDlg, aGEdit.GetPosPixel());
306 aGEdit.Hide();
307 pParaWin->Hide();
308 aFtEditName.Hide();
309 aEdRef.Hide();
310 aRefBtn.Hide();
311
312 pMEdit = aMEFormula.GetEdit();
313 aMEFormula.SetAccessibleName(aFtFormula.GetText());
314 if (pMEdit)
315 pMEdit->SetAccessibleName(aFtFormula.GetText());
316 m_aEditHelpId = pMEdit->GetHelpId();
317 pMEdit->SetUniqueId( m_aEditHelpId );
318
319 bEditFlag=sal_False;
320 bStructUpdate=sal_True;
321 Point aPos=aGEdit.GetPosPixel();
322 pParaWin->SetPosPixel(aPos);
323 pParaWin->SetArgModifiedHdl(LINK( this, FormulaDlg_Impl, ModifyHdl ) );
324 pParaWin->SetFxHdl(LINK( this, FormulaDlg_Impl, FxHdl ) );
325
326 pFuncPage= new FuncPage( &aTabCtrl,_pFunctionMgr);
327 pStructPage= new StructPage( &aTabCtrl);
328 pFuncPage->Hide();
329 pStructPage->Hide();
330 aTabCtrl.SetTabPage( TP_FUNCTION, pFuncPage);
331 aTabCtrl.SetTabPage( TP_STRUCT, pStructPage);
332
333 aOldHelp = pParent->GetHelpId(); // HelpId aus Resource immer fuer "Seite 1"
334 aOldUnique = pParent->GetUniqueId();
335
336 aFtResult.Show( _bSupportResult );
337 aWndResult.Show( _bSupportResult );
338
339 aFtFormResult.Show( _bSupportFunctionResult );
340 aWndFormResult.Show( _bSupportFunctionResult );
341
342 if ( _bSupportMatrix )
343 aBtnMatrix.SetClickHdl(LINK( this, FormulaDlg_Impl, MatrixHdl ) );
344 else
345 aBtnMatrix.Hide();
346
347 aBtnCancel .SetClickHdl( LINK( this, FormulaDlg_Impl, BtnHdl ) );
348 aBtnEnd .SetClickHdl( LINK( this, FormulaDlg_Impl, BtnHdl ) );
349 aBtnForward .SetClickHdl( LINK( this, FormulaDlg_Impl, BtnHdl ) );
350 aBtnBackward.SetClickHdl( LINK( this, FormulaDlg_Impl, BtnHdl ) );
351
352 pFuncPage->SetDoubleClickHdl( LINK( this, FormulaDlg_Impl, DblClkHdl ) );
353 pFuncPage->SetSelectHdl( LINK( this, FormulaDlg_Impl, FuncSelHdl) );
354 pStructPage->SetSelectionHdl( LINK( this, FormulaDlg_Impl, StructSelHdl ) );
355 pMEdit->SetModifyHdl( LINK( this, FormulaDlg_Impl, FormulaHdl ) );
356 aMEFormula.SetSelChangedHdl( LINK( this, FormulaDlg_Impl, FormulaCursorHdl ) );
357
358 aFntLight = aFtFormula.GetFont();
359 aFntLight.SetTransparent( sal_True );
360 aFntBold = aFntLight;
361 aFntBold.SetWeight( WEIGHT_BOLD );
362
363 pParaWin->SetArgumentFonts(aFntBold,aFntLight);
364
365 // function description for choosing a function is no longer in a different color
366
367 aFtHeadLine.SetFont(aFntBold);
368 aFtFuncName.SetFont(aFntLight);
369 aFtFuncDesc.SetFont(aFntLight);
370 }
~FormulaDlg_Impl()371 FormulaDlg_Impl::~FormulaDlg_Impl()
372 {
373 if(aTimer.IsActive())
374 {
375 aTimer.SetTimeoutHdl(Link());
376 aTimer.Stop();
377 } // if(aTimer.IsActive())
378 bIsShutDown=sal_True;// Setzen, damit PreNotify keinen GetFocus speichert.
379 FormEditData* pData = m_pHelper->getFormEditData();
380 if (pData) // wird nicht ueber Close zerstoert;
381 {
382 pData->SetFStart((xub_StrLen)pMEdit->GetSelection().Min());
383 pData->SetSelection(pMEdit->GetSelection());
384
385 if(aTabCtrl.GetCurPageId()==TP_FUNCTION)
386 pData->SetMode( (sal_uInt16) FORMULA_FORMDLG_FORMULA );
387 else
388 pData->SetMode( (sal_uInt16) FORMULA_FORMDLG_EDIT );
389 pData->SetUndoStr(pMEdit->GetText());
390 pData->SetMatrixFlag(aBtnMatrix.IsChecked());
391 }
392
393 aTabCtrl.RemovePage(TP_FUNCTION);
394 aTabCtrl.RemovePage(TP_STRUCT);
395
396 delete pStructPage;
397 delete pFuncPage;
398 delete pParaWin;
399 DeleteArgs();
400 }
401 // -----------------------------------------------------------------------------
PreNotify(NotifyEvent & rNEvt)402 void FormulaDlg_Impl::PreNotify( NotifyEvent& rNEvt )
403 {
404 sal_uInt16 nSwitch=rNEvt.GetType();
405 if(nSwitch==EVENT_GETFOCUS && !bIsShutDown)
406 {
407 Window* pWin=rNEvt.GetWindow();
408 if(pWin!=NULL)
409 {
410 aActivWinId = pWin->GetUniqueId();
411 if(aActivWinId.getLength()==0)
412 {
413 Window* pParent=pWin->GetParent();
414 while(pParent!=NULL)
415 {
416 aActivWinId=pParent->GetUniqueId();
417
418 if(aActivWinId.getLength()!=0) break;
419
420 pParent=pParent->GetParent();
421 }
422 }
423 if(aActivWinId.getLength())
424 {
425
426 FormEditData* pData = m_pHelper->getFormEditData();
427
428 if (pData && !aTimer.IsActive()) // wird nicht ueber Close zerstoert;
429 {
430 pData->SetUniqueId(aActivWinId);
431 }
432 }
433 }
434 }
435 }
GetFormulaOpCodeMapper() const436 uno::Reference< sheet::XFormulaOpCodeMapper > FormulaDlg_Impl::GetFormulaOpCodeMapper() const
437 {
438 if ( !m_xOpCodeMapper.is() )
439 {
440 m_xOpCodeMapper = m_pHelper->getFormulaOpCodeMapper();
441 m_aFunctionOpCodes = m_xOpCodeMapper->getAvailableMappings(sheet::FormulaLanguage::ODFF,sheet::FormulaMapGroup::FUNCTIONS);
442 m_pFunctionOpCodesEnd = m_aFunctionOpCodes.getConstArray() + m_aFunctionOpCodes.getLength();
443
444 m_aUnaryOpCodes = m_xOpCodeMapper->getAvailableMappings(sheet::FormulaLanguage::ODFF,sheet::FormulaMapGroup::UNARY_OPERATORS);
445 m_pUnaryOpCodesEnd = m_aUnaryOpCodes.getConstArray() + m_aUnaryOpCodes.getLength();
446
447 m_aBinaryOpCodes = m_xOpCodeMapper->getAvailableMappings(sheet::FormulaLanguage::ODFF,sheet::FormulaMapGroup::BINARY_OPERATORS);
448 m_pBinaryOpCodesEnd = m_aBinaryOpCodes.getConstArray() + m_aBinaryOpCodes.getLength();
449
450 uno::Sequence< ::rtl::OUString > aArgs(3);
451 aArgs[TOKEN_OPEN] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("("));
452 aArgs[TOKEN_CLOSE] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(")"));
453 aArgs[TOKEN_SEP] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(";"));
454 m_aSeparatorsOpCodes = m_xOpCodeMapper->getMappings(aArgs,sheet::FormulaLanguage::ODFF);
455
456 m_aSpecialOpCodes = m_xOpCodeMapper->getAvailableMappings(sheet::FormulaLanguage::ODFF,sheet::FormulaMapGroup::SPECIAL);
457 m_pSpecialOpCodesEnd = m_aSpecialOpCodes.getConstArray() + m_aSpecialOpCodes.getLength();
458 } // if ( !m_xOpCodeMapper.is() )
459 return m_xOpCodeMapper;
460 }
461
DeleteArgs()462 void FormulaDlg_Impl::DeleteArgs()
463 {
464 ::std::vector< ::rtl::OUString>().swap(m_aArguments);
465 nArgs = 0;
466 }
467 namespace
468 {
469 // comparing two property instances
470 struct OpCodeCompare
471 {
472 typedef sheet::FormulaOpCodeMapEntry first_argument_type;
473 typedef sal_Int32 second_argument_type;
474 typedef bool result_type;
operator ()formula::__anona7d449a50111::OpCodeCompare475 bool operator() (const sheet::FormulaOpCodeMapEntry& x, sal_Int32 y) const
476 {
477 return x.Token.OpCode == y;
478 }
479 };
480 }
481 // -----------------------------------------------------------------------------
GetFunctionPos(xub_StrLen nPos)482 xub_StrLen FormulaDlg_Impl::GetFunctionPos(xub_StrLen nPos)
483 {
484 const sal_Unicode sep = m_pHelper->getFunctionManager()->getSingleToken(IFunctionManager::eSep);
485
486 xub_StrLen nTokPos=1;
487 xub_StrLen nOldTokPos=1;
488 xub_StrLen nFuncPos=STRING_NOTFOUND; //@ Testweise
489 xub_StrLen nPrevFuncPos=1;
490 short nBracketCount=0;
491 sal_Bool bFlag=sal_False;
492 String aFormString = pMEdit->GetText();
493 m_aFormulaHelper.GetCharClass()->toUpper( aFormString );
494
495 if ( m_aTokenList.getLength() )
496 {
497 const uno::Reference< sheet::XFormulaParser > xParser(m_pHelper->getFormulaParser());
498 const table::CellAddress aRefPos(m_pHelper->getReferencePosition());
499
500 const sheet::FormulaToken* pIter = m_aTokenList.getConstArray();
501 const sheet::FormulaToken* pEnd = pIter + m_aTokenList.getLength();
502 //if ( pIter != pEnd && aFormString.GetChar(0) == '=' )
503 // ++pIter;
504 try
505 {
506 while ( pIter != pEnd )
507 {
508 const sal_Int32 eOp = pIter->OpCode;
509 uno::Sequence<sheet::FormulaToken> aArgs(1);
510 aArgs[0] = *pIter;
511 const String aString = xParser->printFormula(aArgs, aRefPos);
512 const sheet::FormulaToken* pNextToken = pIter + 1;
513
514 if(!bUserMatrixFlag && FormulaCompiler::IsMatrixFunction((OpCode)eOp) )
515 {
516 aBtnMatrix.Check();
517 }
518
519 if ( eOp == m_aSpecialOpCodes[sheet::FormulaMapGroupSpecialOffset::PUSH].Token.OpCode || eOp == m_aSpecialOpCodes[sheet::FormulaMapGroupSpecialOffset::SPACES].Token.OpCode )
520 {
521 const xub_StrLen n1=aFormString.Search(sep, nTokPos);
522 const xub_StrLen n2=aFormString.Search(')',nTokPos);
523 xub_StrLen nXXX=nTokPos;
524 if(n1<n2)
525 {
526 nTokPos=n1;
527 }
528 else
529 {
530 nTokPos=n2;
531 }
532 if ( pNextToken != pEnd )
533 {
534 aArgs[0] = *pNextToken;
535 const String a2String = xParser->printFormula(aArgs, aRefPos);
536 const xub_StrLen n3 = aFormString.Search(a2String,nXXX);
537 if ( n3 < nTokPos )
538 nTokPos = n3;
539 }
540 }
541 else
542 {
543 nTokPos = sal::static_int_cast<xub_StrLen>( nTokPos + aString.Len() );
544 }
545
546 if ( eOp == m_aSeparatorsOpCodes[TOKEN_OPEN].OpCode )
547 {
548 nBracketCount++;
549 bFlag=sal_True;
550 }
551 else if ( eOp == m_aSeparatorsOpCodes[TOKEN_CLOSE].OpCode )
552 {
553 nBracketCount--;
554 bFlag=sal_False;
555 nFuncPos=nPrevFuncPos;
556 }
557 bool bIsFunction = ::std::find_if(m_aFunctionOpCodes.getConstArray(),m_pFunctionOpCodesEnd,::std::bind(OpCodeCompare(), ::std::placeholders::_1,boost::cref(eOp))) != m_pFunctionOpCodesEnd;
558
559 if ( bIsFunction && m_aSpecialOpCodes[sheet::FormulaMapGroupSpecialOffset::SPACES].Token.OpCode != eOp )
560 {
561 nPrevFuncPos = nFuncPos;
562 nFuncPos = nOldTokPos;
563 }
564
565 if ( nOldTokPos <= nPos && nPos < nTokPos )
566 {
567 if ( !bIsFunction )
568 {
569 if ( nBracketCount < 1 )
570 {
571 nFuncPos= pMEdit->GetText().Len();
572 }
573 else if ( !bFlag )
574 {
575 nFuncPos=nPrevFuncPos;
576 }
577 }
578 break;
579 }
580
581 pIter = pNextToken;
582 nOldTokPos = nTokPos;
583 } // while ( pIter != pEnd )
584 }
585 catch(const uno::Exception& )
586 {
587 DBG_ERROR("Exception caught!");
588 }
589 }
590
591 return nFuncPos;
592 }
593 // -----------------------------------------------------------------------------
CalcValue(const String & rStrExp,String & rStrResult)594 sal_Bool FormulaDlg_Impl::CalcValue( const String& rStrExp, String& rStrResult )
595 {
596 sal_Bool bResult = sal_True;
597
598 if ( rStrExp.Len() > 0 )
599 {
600 // nur, wenn keine Tastatureingabe mehr anliegt, den Wert berechnen:
601
602 if ( !Application::AnyInput( INPUT_KEYBOARD ) )
603 {
604 bResult = m_pHelper->calculateValue(rStrExp,rStrResult);
605 }
606 else
607 bResult = sal_False;
608 }
609
610 return bResult;
611 }
612
UpdateValues()613 void FormulaDlg_Impl::UpdateValues()
614 {
615 String aStrResult;
616
617 if ( CalcValue( pFuncDesc->getFormula( m_aArguments ), aStrResult ) )
618 aWndResult.SetValue( aStrResult );
619
620 aStrResult.Erase();
621 if ( CalcValue(m_pHelper->getCurrentFormula(), aStrResult ) )
622 aWndFormResult.SetValue( aStrResult );
623 else
624 {
625 aStrResult.Erase();
626 aWndFormResult.SetValue( aStrResult );
627 }
628 CalcStruct(pMEdit->GetText());
629 }
630
CalcStruct(const String & rStrExp)631 sal_Bool FormulaDlg_Impl::CalcStruct( const String& rStrExp)
632 {
633 sal_Bool bResult = sal_True;
634 xub_StrLen nLength=rStrExp.Len();
635
636 if ( rStrExp.Len() > 0 && aOldFormula!=rStrExp && bStructUpdate)
637 {
638 // nur, wenn keine Tastatureingabe mehr anliegt, den Wert berechnen:
639
640 if ( !Application::AnyInput( INPUT_KEYBOARD ) )
641 {
642 pStructPage->ClearStruct();
643
644 String aString=rStrExp;
645 if(rStrExp.GetChar(nLength-1)=='(')
646 {
647 aString.Erase((xub_StrLen)(nLength-1));
648 }
649
650 aString.EraseAllChars('\n');
651 String aStrResult;
652
653 if ( CalcValue(aString, aStrResult ) )
654 aWndFormResult.SetValue( aStrResult );
655
656 UpdateTokenArray(aString);
657 fillTree(pStructPage);
658
659 aOldFormula=rStrExp;
660 if(rStrExp.GetChar(nLength-1)=='(')
661 UpdateTokenArray(rStrExp);
662 }
663 else
664 bResult = sal_False;
665 }
666 return bResult;
667 }
668
669 // -----------------------------------------------------------------------------
MakeTree(IStructHelper * _pTree,SvLBoxEntry * pParent,FormulaToken * _pToken,long Count)670 void FormulaDlg_Impl::MakeTree(IStructHelper* _pTree,SvLBoxEntry* pParent,FormulaToken* _pToken,long Count)
671 {
672 if( _pToken != NULL && Count > 0 )
673 {
674 long nParas = _pToken->GetParamCount();
675 OpCode eOp = _pToken->GetOpCode();
676
677 // #i101512# for output, the original token is needed
678 FormulaToken* pOrigToken = (_pToken->GetType() == svFAP) ? _pToken->GetFAPOrigToken() : _pToken;
679 uno::Sequence<sheet::FormulaToken> aArgs(1);
680 aArgs[0] = m_aTokenMap.find(pOrigToken)->second;
681 try
682 {
683 const table::CellAddress aRefPos(m_pHelper->getReferencePosition());
684 const String aResult = m_pHelper->getFormulaParser()->printFormula(aArgs, aRefPos);
685
686 if ( nParas > 0 )
687 {
688 SvLBoxEntry* pEntry;
689
690 String aTest=_pTree->GetEntryText(pParent);
691
692 if(aTest==aResult &&
693 (eOp==ocAdd || eOp==ocMul ||
694 eOp==ocAmpersand))
695 {
696 pEntry=pParent;
697 }
698 else
699 {
700 if(eOp==ocBad)
701 {
702 pEntry=_pTree->InsertEntry(aResult,pParent,STRUCT_ERROR,0,_pToken);
703 }
704 else
705 {
706 pEntry=_pTree->InsertEntry(aResult,pParent,STRUCT_FOLDER,0,_pToken);
707 }
708 }
709
710 MakeTree(_pTree,pEntry,m_pTokenArray->PrevRPN(),nParas);
711 --Count;
712 m_pTokenArray->NextRPN();
713 MakeTree(_pTree,pParent,m_pTokenArray->PrevRPN(),Count);
714 }
715 else
716 {
717 if(eOp==ocBad)
718 {
719 _pTree->InsertEntry(aResult,pParent,STRUCT_ERROR,0,_pToken);
720 }
721 else
722 {
723 _pTree->InsertEntry(aResult,pParent,STRUCT_END,0,_pToken);
724 }
725 --Count;
726 MakeTree(_pTree,pParent,m_pTokenArray->PrevRPN(),Count);
727 }
728 }
729 catch(uno::Exception&)
730 {
731 DBG_UNHANDLED_EXCEPTION();
732 }
733 }
734 }
735
fillTree(IStructHelper * _pTree)736 void FormulaDlg_Impl::fillTree(IStructHelper* _pTree)
737 {
738 GetFormulaOpCodeMapper();
739 FormulaToken* pToken = m_pTokenArray->LastRPN();
740
741 if( pToken != NULL)
742 {
743 MakeTree(_pTree,NULL,pToken,1);
744 }
745 }
UpdateTokenArray(const String & rStrExp)746 void FormulaDlg_Impl::UpdateTokenArray( const String& rStrExp)
747 {
748 m_aTokenMap.clear();
749 m_aTokenList.realloc(0);
750 try
751 {
752 const table::CellAddress aRefPos(m_pHelper->getReferencePosition());
753 m_aTokenList = m_pHelper->getFormulaParser()->parseFormula(rStrExp, aRefPos);
754 }
755 catch(const uno::Exception&)
756 {
757 DBG_UNHANDLED_EXCEPTION();
758 }
759 GetFormulaOpCodeMapper(); // just to get it initialized
760 m_pTokenArray = m_pHelper->convertToTokenArray(m_aTokenList);
761 const sal_Int32 nLen = static_cast<sal_Int32>(m_pTokenArray->GetLen());
762 FormulaToken** pTokens = m_pTokenArray->GetArray();
763 if ( pTokens && nLen == m_aTokenList.getLength() )
764 {
765 for (sal_Int32 nPos=0; nPos<nLen; nPos++)
766 {
767 m_aTokenMap.insert(::std::map<FormulaToken*,sheet::FormulaToken>::value_type(pTokens[nPos],m_aTokenList[nPos]));
768 }
769 } // if ( pTokens && nLen == m_aTokenList.getLength() )
770
771 FormulaCompiler aCompiler(*m_pTokenArray.get());
772 aCompiler.SetCompileForFAP(sal_True); // #i101512# special handling is needed
773 aCompiler.CompileTokenArray();
774 }
775
FillDialog(sal_Bool nFlag)776 void FormulaDlg_Impl::FillDialog(sal_Bool nFlag)
777 {
778 sal_Bool bNext=sal_True, bPrev=sal_True;
779 if(nFlag)
780 FillControls(bNext, bPrev);
781 FillListboxes();
782 if(nFlag)
783 {
784 aBtnBackward.Enable(bPrev);
785 aBtnForward.Enable(bNext);
786 }
787
788 String aStrResult;
789
790 if ( CalcValue(m_pHelper->getCurrentFormula(), aStrResult ) )
791 aWndFormResult.SetValue( aStrResult );
792 else
793 {
794 aStrResult.Erase();
795 aWndFormResult.SetValue( aStrResult );
796 }
797 }
798
799 // -----------------------------------------------------------------------------
FillListboxes()800 void FormulaDlg_Impl::FillListboxes()
801 {
802 // Umschalten zwischen den "Seiten"
803 FormEditData* pData = m_pHelper->getFormEditData();
804 String aNewTitle;
805 // 1. Seite: Funktion auswaehlen
806 if ( pFuncDesc && pFuncDesc->getCategory() )
807 {
808 if( pFuncPage->GetCategory() != pFuncDesc->getCategory()->getNumber() + 1 )
809 pFuncPage->SetCategory(static_cast<sal_uInt16>(pFuncDesc->getCategory()->getNumber() + 1));
810
811 sal_uInt16 nPos=pFuncPage->GetFuncPos(pFuncDesc);
812
813 pFuncPage->SetFunction(nPos);
814 }
815 else if ( pData )
816 {
817 pFuncPage->SetCategory( pData->GetCatSel() );
818 pFuncPage->SetFunction( pData->GetFuncSel() );
819 }
820 FuncSelHdl(NULL);
821
822 // ResizeArgArr jetzt schon in UpdateFunctionDesc
823
824
825 m_pHelper->setDispatcherLock( sal_True);// Modal-Modus einschalten
826
827 aNewTitle = aTitle1;
828
829 // HelpId fuer 1. Seite ist die aus der Resource
830 m_pParent->SetHelpId( aOldHelp );
831 m_pParent->SetUniqueId( aOldUnique );
832 }
833 // -----------------------------------------------------------------------------
FillControls(sal_Bool & rbNext,sal_Bool & rbPrev)834 void FormulaDlg_Impl::FillControls(sal_Bool &rbNext, sal_Bool &rbPrev)
835 {
836 // Umschalten zwischen den "Seiten"
837 FormEditData* pData = m_pHelper->getFormEditData();
838 if (!pData )
839 return;
840
841 String aNewTitle;
842 // 2. Seite oder Editieren: ausgewaehlte Funktion anzeigen
843
844 xub_StrLen nFStart = pData->GetFStart();
845 String aFormula = m_pHelper->getCurrentFormula();
846 xub_StrLen nNextFStart = nFStart;
847 xub_StrLen nNextFEnd = 0;
848
849 aFormula.AppendAscii(RTL_CONSTASCII_STRINGPARAM( " )" ));
850 DeleteArgs();
851 const IFunctionDescription* pOldFuncDesc = pFuncDesc;
852 sal_Bool bTestFlag = sal_False;
853
854 if ( m_aFormulaHelper.GetNextFunc( aFormula, sal_False,
855 nNextFStart, &nNextFEnd, &pFuncDesc, &m_aArguments ) )
856 {
857 bTestFlag = (pOldFuncDesc != pFuncDesc);
858 if(bTestFlag)
859 {
860 aFtHeadLine.Hide();
861 aFtFuncName.Hide();
862 aFtFuncDesc.Hide();
863 pParaWin->SetFunctionDesc(pFuncDesc);
864 aFtEditName.SetText( pFuncDesc->getFunctionName() );
865 aFtEditName.Show();
866 pParaWin->Show();
867 const rtl::OString aHelpId = pFuncDesc->getHelpId();
868 if ( aHelpId.getLength() )
869 pMEdit->SetHelpId(aHelpId);
870 }
871
872 xub_StrLen nOldStart, nOldEnd;
873 m_pHelper->getSelection( nOldStart, nOldEnd );
874 if ( nOldStart != nNextFStart || nOldEnd != nNextFEnd )
875 {
876 m_pHelper->setSelection( nNextFStart, nNextFEnd );
877 }
878 aFuncSel.Min() = nNextFStart;
879 aFuncSel.Max() = nNextFEnd;
880
881 if(!bEditFlag)
882 pMEdit->SetText(m_pHelper->getCurrentFormula());
883 xub_StrLen PrivStart, PrivEnd;
884 m_pHelper->getSelection( PrivStart, PrivEnd);
885 if(!bEditFlag)
886 pMEdit->SetSelection( Selection(PrivStart, PrivEnd));
887
888 nArgs = pFuncDesc->getSuppressedArgumentCount();
889 sal_uInt16 nOffset = pData->GetOffset();
890 nEdFocus = pData->GetEdFocus();
891
892 // Verkettung der Edit's fuer Focus-Kontrolle
893
894 if(bTestFlag)
895 pParaWin->SetArgumentOffset(nOffset);
896 sal_uInt16 nActiv=0;
897 xub_StrLen nArgPos= m_aFormulaHelper.GetArgStart( aFormula, nFStart, 0 );
898 xub_StrLen nEditPos=(xub_StrLen) pMEdit->GetSelection().Min();
899 sal_Bool bFlag=sal_False;
900
901 for(sal_uInt16 i=0;i<nArgs;i++)
902 {
903 sal_Int32 nLength = m_aArguments[i].getLength()+1;
904 pParaWin->SetArgument(i,m_aArguments[i]);
905 if(nArgPos<=nEditPos && nEditPos<nArgPos+nLength)
906 {
907 nActiv=i;
908 bFlag=sal_True;
909 }
910 nArgPos = sal::static_int_cast<xub_StrLen>( nArgPos + nLength );
911 }
912 pParaWin->UpdateParas();
913
914 if(bFlag)
915 {
916 pParaWin->SetActiveLine(nActiv);
917 }
918
919 //pParaWin->SetEdFocus( nEdFocus );
920 UpdateValues();
921 }
922 else
923 {
924 aFtEditName.SetText(String());
925 pMEdit->SetHelpId( m_aEditHelpId );
926 }
927 // Test, ob vorne/hinten noch mehr Funktionen sind
928
929 xub_StrLen nTempStart = m_aFormulaHelper.GetArgStart( aFormula, nFStart, 0 );
930 rbNext = m_aFormulaHelper.GetNextFunc( aFormula, sal_False, nTempStart );
931 nTempStart=(xub_StrLen)pMEdit->GetSelection().Min();
932 pData->SetFStart(nTempStart);
933 rbPrev = m_aFormulaHelper.GetNextFunc( aFormula, sal_True, nTempStart );
934 }
935 // -----------------------------------------------------------------------------
936
ClearAllParas()937 void FormulaDlg_Impl::ClearAllParas()
938 {
939 DeleteArgs();
940 pFuncDesc = NULL;
941 pParaWin->ClearAll();
942 aWndResult.SetValue(String());
943 aFtFuncName.SetText(String());
944 FuncSelHdl(NULL);
945
946 if(pFuncPage->IsVisible())
947 {
948 aFtEditName.Hide();
949 pParaWin->Hide();
950
951 aBtnForward.Enable(sal_True); //@new
952 aFtHeadLine.Show();
953 aFtFuncName.Show();
954 aFtFuncDesc.Show();
955 }
956 }
RepairFormula(const String & aFormula)957 String FormulaDlg_Impl::RepairFormula(const String& aFormula)
958 {
959 String aResult('=');
960 try
961 {
962 UpdateTokenArray(aFormula);
963
964 if ( m_aTokenList.getLength() )
965 {
966 const table::CellAddress aRefPos(m_pHelper->getReferencePosition());
967 const String sFormula(m_pHelper->getFormulaParser()->printFormula(m_aTokenList, aRefPos));
968 if ( !sFormula.Len() || sFormula.GetChar(0) != '=' )
969 aResult += sFormula;
970 else
971 aResult = sFormula;
972
973 }
974 }
975 catch(const uno::Exception& )
976 {
977 DBG_ERROR("Exception caught!");
978 }
979 return aResult;
980 }
981
DoEnter(sal_Bool bOk)982 void FormulaDlg_Impl::DoEnter(sal_Bool bOk)
983 {
984 // Eingabe ins Dokument uebernehmen oder abbrechen
985 if ( bOk)
986 {
987 // ggf. Dummy-Argumente entfernen
988 String aInputFormula = m_pHelper->getCurrentFormula();
989 String aString = RepairFormula(pMEdit->GetText());
990 m_pHelper->setSelection(0, aInputFormula.Len());
991 m_pHelper->setCurrentFormula(aString);
992 }
993
994 m_pHelper->switchBack();
995
996 m_pHelper->dispatch(bOk,aBtnMatrix.IsChecked());
997 // Daten loeschen
998 m_pHelper->deleteFormData();
999
1000 // Dialog schliessen
1001 m_pHelper->doClose(bOk);
1002 }
1003 // -----------------------------------------------------------------------------
1004
IMPL_LINK(FormulaDlg_Impl,BtnHdl,PushButton *,pBtn)1005 IMPL_LINK( FormulaDlg_Impl, BtnHdl, PushButton*, pBtn )
1006 {
1007 if ( pBtn == &aBtnCancel )
1008 {
1009 DoEnter(sal_False); // schliesst den Dialog
1010 }
1011 else if ( pBtn == &aBtnEnd )
1012 {
1013 DoEnter(sal_True); // schliesst den Dialog
1014 }
1015 else if ( pBtn == &aBtnForward )
1016 {
1017 //@pMEdit->GrabFocus(); // Damit die Selektion auch angezeigt wird.
1018 const IFunctionDescription* pDesc =pFuncPage->GetFuncDesc( pFuncPage->GetFunction() );
1019
1020 if(pDesc==pFuncDesc || !pFuncPage->IsVisible())
1021 EditNextFunc( sal_True );
1022 else
1023 {
1024 DblClkHdl(pFuncPage); //new
1025 aBtnForward.Enable(sal_False); //new
1026 }
1027 //@EditNextFunc( sal_True );
1028 }
1029 else if ( pBtn == &aBtnBackward )
1030 {
1031 bEditFlag=sal_False;
1032 aBtnForward.Enable(sal_True);
1033 EditNextFunc( sal_False );
1034 aMEFormula.Invalidate();
1035 aMEFormula.Update();
1036 }
1037 //...
1038
1039 return 0;
1040 }
1041 // -----------------------------------------------------------------------------
1042
1043
1044 // --------------------------------------------------------------------------
1045 // Funktionen fuer 1. Seite
1046 // --------------------------------------------------------------------------
1047
ResizeArgArr(const IFunctionDescription * pNewFunc)1048 void FormulaDlg_Impl::ResizeArgArr( const IFunctionDescription* pNewFunc )
1049 {
1050 if ( pFuncDesc != pNewFunc )
1051 {
1052 DeleteArgs();
1053
1054 if ( pNewFunc )
1055 nArgs = pNewFunc->getSuppressedArgumentCount();
1056
1057 pFuncDesc = pNewFunc;
1058 }
1059 }
1060 // -----------------------------------------------------------------------------
1061
UpdateFunctionDesc()1062 void FormulaDlg_Impl::UpdateFunctionDesc()
1063 {
1064 FormEditData* pData = m_pHelper->getFormEditData();
1065 if (!pData)
1066 return;
1067 sal_uInt16 nCat = pFuncPage->GetCategory();
1068 if ( nCat == LISTBOX_ENTRY_NOTFOUND )
1069 nCat = 0;
1070 pData->SetCatSel( nCat );
1071 sal_uInt16 nFunc = pFuncPage->GetFunction();
1072 if ( nFunc == LISTBOX_ENTRY_NOTFOUND )
1073 nFunc = 0;
1074 pData->SetFuncSel( nFunc );
1075
1076 if ( (pFuncPage->GetFunctionEntryCount() > 0)
1077 && (pFuncPage->GetFunction() != LISTBOX_ENTRY_NOTFOUND) )
1078 {
1079 const IFunctionDescription* pDesc = pFuncPage->GetFuncDesc(pFuncPage->GetFunction() );
1080 if (pDesc)
1081 {
1082 pDesc->initArgumentInfo(); // full argument info is needed
1083
1084 String aSig = pDesc->getSignature();
1085
1086 aFtFuncName.SetText( aSig );
1087 aFtFuncDesc.SetText( pDesc->getDescription() );
1088 ResizeArgArr( pDesc );
1089
1090 if ( !m_aArguments.empty() ) // noch Argumente da?
1091 aSig = pDesc->getFormula( m_aArguments ); // fuer Eingabezeile
1092 //@ m_pHelper->setCurrentFormula( aSig );
1093 }
1094 }
1095 else
1096 {
1097 aFtFuncName.SetText( String() );
1098 aFtFuncDesc.SetText( String() );
1099
1100 //ResizeArgArr( NULL );
1101 m_pHelper->setCurrentFormula( String() );
1102 }
1103 }
1104 // -----------------------------------------------------------------------------
1105
1106 // Handler fuer Listboxen
1107
IMPL_LINK(FormulaDlg_Impl,DblClkHdl,FuncPage *,EMPTYARG)1108 IMPL_LINK( FormulaDlg_Impl, DblClkHdl, FuncPage*, EMPTYARG )
1109 {
1110 sal_uInt16 nFunc = pFuncPage->GetFunction();
1111
1112 // ex-UpdateLRUList
1113 const IFunctionDescription* pDesc = pFuncPage->GetFuncDesc(nFunc);
1114 m_pHelper->insertEntryToLRUList(pDesc);
1115
1116 String aFuncName = pFuncPage->GetSelFunctionName();
1117 aFuncName.AppendAscii(RTL_CONSTASCII_STRINGPARAM( "()" ));
1118 m_pHelper->setCurrentFormula(aFuncName);
1119 pMEdit->ReplaceSelected(aFuncName);
1120
1121 Selection aSel=pMEdit->GetSelection();
1122 aSel.Max()=aSel.Max()-1;
1123 pMEdit->SetSelection(aSel);
1124
1125 FormulaHdl(pMEdit);
1126
1127 aSel.Min()=aSel.Max();
1128 pMEdit->SetSelection(aSel);
1129
1130 if(nArgs==0)
1131 {
1132 BtnHdl(&aBtnBackward);
1133 }
1134
1135 pParaWin->SetEdFocus(0);
1136 aBtnForward.Enable(sal_False); //@New
1137
1138 return 0;
1139 }
1140 // -----------------------------------------------------------------------------
1141
1142 // --------------------------------------------------------------------------
1143 // Funktionen fuer rechte Seite
1144 // --------------------------------------------------------------------------
SetData(xub_StrLen nFStart,xub_StrLen nNextFStart,xub_StrLen nNextFEnd,xub_StrLen & PrivStart,xub_StrLen & PrivEnd)1145 void FormulaDlg_Impl::SetData(xub_StrLen nFStart,xub_StrLen nNextFStart,xub_StrLen nNextFEnd,xub_StrLen& PrivStart,xub_StrLen& PrivEnd)
1146 {
1147 xub_StrLen nFEnd;
1148
1149 // Selektion merken und neue setzen
1150 m_pHelper->getSelection( nFStart, nFEnd );
1151 m_pHelper->setSelection( nNextFStart, nNextFEnd );
1152 if(!bEditFlag)
1153 pMEdit->SetText(m_pHelper->getCurrentFormula());
1154
1155
1156 m_pHelper->getSelection( PrivStart, PrivEnd);
1157 if(!bEditFlag)
1158 {
1159 pMEdit->SetSelection( Selection(PrivStart, PrivEnd));
1160 aMEFormula.UpdateOldSel();
1161 }
1162
1163 FormEditData* pData = m_pHelper->getFormEditData();
1164 pData->SetFStart( nNextFStart );
1165 pData->SetOffset( 0 );
1166 pData->SetEdFocus( 0 );
1167
1168 FillDialog();
1169 }
1170 // -----------------------------------------------------------------------------
EditThisFunc(xub_StrLen nFStart)1171 void FormulaDlg_Impl::EditThisFunc(xub_StrLen nFStart)
1172 {
1173 FormEditData* pData = m_pHelper->getFormEditData();
1174 if (!pData) return;
1175
1176 String aFormula = m_pHelper->getCurrentFormula();
1177
1178 if(nFStart==NOT_FOUND)
1179 {
1180 nFStart = pData->GetFStart();
1181 }
1182 else
1183 {
1184 pData->SetFStart(nFStart);
1185 }
1186
1187 xub_StrLen nNextFStart = nFStart;
1188 xub_StrLen nNextFEnd = 0;
1189
1190 sal_Bool bFound;
1191
1192 //@bFound = m_pHelper->getNextFunction( aFormula, sal_False, nNextFStart, &nNextFEnd, &pFuncDesc );
1193
1194 bFound = m_aFormulaHelper.GetNextFunc( aFormula, sal_False, nNextFStart, &nNextFEnd);
1195 if ( bFound )
1196 {
1197 xub_StrLen PrivStart, PrivEnd;
1198 SetData(nFStart,nNextFStart,nNextFEnd,PrivStart, PrivEnd);
1199 m_pHelper->showReference(aFormula.Copy(PrivStart, PrivEnd-PrivStart));
1200 }
1201 else
1202 {
1203 ClearAllParas();
1204 }
1205 }
1206
EditNextFunc(sal_Bool bForward,xub_StrLen nFStart)1207 void FormulaDlg_Impl::EditNextFunc( sal_Bool bForward, xub_StrLen nFStart )
1208 {
1209 FormEditData* pData = m_pHelper->getFormEditData();
1210 if (!pData)
1211 return;
1212
1213 String aFormula = m_pHelper->getCurrentFormula();
1214
1215 if(nFStart==NOT_FOUND)
1216 {
1217 nFStart = pData->GetFStart();
1218 }
1219 else
1220 {
1221 pData->SetFStart(nFStart);
1222 }
1223
1224 xub_StrLen nNextFStart = 0;
1225 xub_StrLen nNextFEnd = 0;
1226
1227 sal_Bool bFound;
1228 if ( bForward )
1229 {
1230 nNextFStart = m_aFormulaHelper.GetArgStart( aFormula, nFStart, 0 );
1231 //@bFound = m_pHelper->getNextFunction( aFormula, sal_False, nNextFStart, &nNextFEnd, &pFuncDesc );
1232 bFound = m_aFormulaHelper.GetNextFunc( aFormula, sal_False, nNextFStart, &nNextFEnd);
1233 }
1234 else
1235 {
1236 nNextFStart = nFStart;
1237 //@bFound = m_pHelper->getNextFunction( aFormula, sal_True, nNextFStart, &nNextFEnd, &pFuncDesc );
1238 bFound = m_aFormulaHelper.GetNextFunc( aFormula, sal_True, nNextFStart, &nNextFEnd);
1239 }
1240
1241 if ( bFound )
1242 {
1243 xub_StrLen PrivStart, PrivEnd;
1244 SetData(nFStart,nNextFStart,nNextFEnd,PrivStart, PrivEnd);
1245 }
1246 }
1247
EditFuncParas(xub_StrLen nEditPos)1248 void FormulaDlg_Impl::EditFuncParas(xub_StrLen nEditPos)
1249 {
1250 if(pFuncDesc!=NULL)
1251 {
1252 FormEditData* pData = m_pHelper->getFormEditData();
1253 if (!pData) return;
1254
1255 String aFormula = m_pHelper->getCurrentFormula();
1256 aFormula +=')';
1257 xub_StrLen nFStart = pData->GetFStart();
1258
1259 DeleteArgs();
1260
1261 nArgs = pFuncDesc->getSuppressedArgumentCount();
1262
1263 sal_Int32 nArgPos=m_aFormulaHelper.GetArgStart( aFormula, nFStart, 0 );
1264 m_aFormulaHelper.GetArgStrings(m_aArguments,aFormula, nFStart, nArgs );
1265 // m_aArguments = ScFormulaUtil::GetArgStrings( aFormula, nFStart, nArgs );
1266
1267 sal_uInt16 nActiv=pParaWin->GetSliderPos();
1268 sal_Bool bFlag=sal_False;
1269 ::std::vector< ::rtl::OUString >::iterator aIter = m_aArguments.begin();
1270 ::std::vector< ::rtl::OUString >::iterator aEnd = m_aArguments.end();
1271 for(sal_uInt16 i=0;aIter != aEnd;i++,++aIter)
1272 {
1273 sal_Int32 nLength=(*aIter).getLength();
1274 pParaWin->SetArgument(i,(*aIter));
1275 if(nArgPos<=nEditPos && nEditPos<nArgPos+nLength)
1276 {
1277 nActiv=i;
1278 bFlag=sal_True;
1279 }
1280 nArgPos+=nLength+1;
1281 }
1282
1283 if(bFlag)
1284 {
1285 pParaWin->SetSliderPos(nActiv);
1286 }
1287
1288 pParaWin->UpdateParas();
1289 UpdateValues();
1290 }
1291
1292 }
1293
SaveArg(sal_uInt16 nEd)1294 void FormulaDlg_Impl::SaveArg( sal_uInt16 nEd )
1295 {
1296 if (nEd<nArgs)
1297 {
1298 sal_uInt16 i;
1299 for(i=0;i<=nEd;i++)
1300 {
1301 if ( m_aArguments[i].getLength() == 0 )
1302 m_aArguments[i] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" "));
1303 }
1304 if(pParaWin->GetArgument(nEd).Len()!=0)
1305 m_aArguments[nEd] = pParaWin->GetArgument(nEd);
1306
1307 sal_uInt16 nClearPos=nEd+1;
1308 for(i=nEd+1;i<nArgs;i++)
1309 {
1310 if(pParaWin->GetArgument(i).Len()!=0)
1311 {
1312 nClearPos=i+1;
1313 }
1314 }
1315
1316 for(i=nClearPos;i<nArgs;i++)
1317 {
1318 m_aArguments[i] = ::rtl::OUString();
1319 }
1320 }
1321 }
1322
IMPL_LINK(FormulaDlg_Impl,FxHdl,ParaWin *,pPtr)1323 IMPL_LINK( FormulaDlg_Impl, FxHdl, ParaWin*, pPtr )
1324 {
1325 if(pPtr==pParaWin)
1326 {
1327 aBtnForward.Enable(sal_True); //@ Damit eine neue Fkt eingegeben werden kann.
1328 aTabCtrl.SetCurPageId(TP_FUNCTION);
1329
1330 String aUndoStr = m_pHelper->getCurrentFormula(); // bevor unten ein ";" eingefuegt wird
1331 FormEditData* pData = m_pHelper->getFormEditData();
1332 if (!pData) return 0;
1333
1334 sal_uInt16 nArgNo = pParaWin->GetActiveLine();
1335 nEdFocus=nArgNo;
1336
1337 SaveArg(nArgNo);
1338 UpdateSelection();
1339
1340 xub_StrLen nFormulaStrPos = pData->GetFStart();
1341
1342 String aFormula = m_pHelper->getCurrentFormula();
1343 xub_StrLen n1 = m_aFormulaHelper.GetArgStart( aFormula, nFormulaStrPos, nEdFocus+pData->GetOffset() );
1344
1345 pData->SetEdFocus( nEdFocus );
1346 pData->SaveValues();
1347 pData->SetMode( (sal_uInt16) FORMULA_FORMDLG_FORMULA );
1348 pData->SetFStart( n1 );
1349 pData->SetUndoStr( aUndoStr );
1350 ClearAllParas();
1351
1352 FillDialog(sal_False);
1353 pFuncPage->SetFocus(); //Da Parawin nicht mehr sichtbar
1354 }
1355 return 0;
1356 }
1357
IMPL_LINK(FormulaDlg_Impl,ModifyHdl,ParaWin *,pPtr)1358 IMPL_LINK( FormulaDlg_Impl, ModifyHdl, ParaWin*, pPtr )
1359 {
1360 if(pPtr==pParaWin)
1361 {
1362 SaveArg(pParaWin->GetActiveLine());
1363 UpdateValues();
1364
1365 UpdateSelection();
1366 CalcStruct(pMEdit->GetText());
1367 }
1368 return 0;
1369 }
1370
IMPL_LINK(FormulaDlg_Impl,FormulaHdl,MultiLineEdit *,EMPTYARG)1371 IMPL_LINK( FormulaDlg_Impl, FormulaHdl, MultiLineEdit*, EMPTYARG )
1372 {
1373
1374 FormEditData* pData = m_pHelper->getFormEditData();
1375 if (!pData) return 0;
1376
1377 bEditFlag=sal_True;
1378 String aInputFormula=m_pHelper->getCurrentFormula();
1379 String aString=pMEdit->GetText();
1380
1381 Selection aSel =pMEdit->GetSelection();
1382 xub_StrLen nTest=0;
1383
1384 if(aString.Len()==0) //falls alles geloescht wurde
1385 {
1386 aString +='=';
1387 pMEdit->SetText(aString);
1388 aSel .Min()=1;
1389 aSel .Max()=1;
1390 pMEdit->SetSelection(aSel);
1391 }
1392 else if(aString.GetChar(nTest)!='=') //falls ersetzt wurde;
1393 {
1394 aString.Insert( (sal_Unicode)'=', 0 );
1395 pMEdit->SetText(aString);
1396 aSel .Min()+=1;
1397 aSel .Max()+=1;
1398 pMEdit->SetSelection(aSel);
1399 }
1400
1401
1402 m_pHelper->setSelection(0, aInputFormula.Len());
1403 m_pHelper->setCurrentFormula(aString);
1404 m_pHelper->setSelection((xub_StrLen)aSel.Min(),(xub_StrLen)aSel.Max());
1405
1406 xub_StrLen nPos=(xub_StrLen)aSel.Min()-1;
1407
1408 String aStrResult;
1409
1410 if ( CalcValue(m_pHelper->getCurrentFormula(), aStrResult ) )
1411 aWndFormResult.SetValue( aStrResult );
1412 else
1413 {
1414 aStrResult.Erase();
1415 aWndFormResult.SetValue( aStrResult );
1416 }
1417 CalcStruct(aString);
1418
1419 nPos=GetFunctionPos(nPos);
1420
1421 if(nPos<aSel.Min()-1)
1422 {
1423 xub_StrLen nPos1=aString.Search('(',nPos);
1424 EditNextFunc( sal_False, nPos1);
1425 }
1426 else
1427 {
1428 ClearAllParas();
1429 }
1430
1431 m_pHelper->setSelection((xub_StrLen)aSel.Min(),(xub_StrLen)aSel.Max());
1432 bEditFlag=sal_False;
1433 return 0;
1434 }
1435
IMPL_LINK(FormulaDlg_Impl,FormulaCursorHdl,EditBox *,EMPTYARG)1436 IMPL_LINK( FormulaDlg_Impl, FormulaCursorHdl, EditBox*, EMPTYARG )
1437 {
1438 FormEditData* pData = m_pHelper->getFormEditData();
1439 if (!pData) return 0;
1440 xub_StrLen nFStart = pData->GetFStart();
1441
1442 bEditFlag=sal_True;
1443
1444 String aInputFormula=m_pHelper->getCurrentFormula();
1445 String aString=pMEdit->GetText();
1446
1447 Selection aSel =pMEdit->GetSelection();
1448 m_pHelper->setSelection((xub_StrLen)aSel.Min(),(xub_StrLen)aSel.Max());
1449
1450 if(aSel.Min()==0)
1451 {
1452 aSel.Min()=1;
1453 pMEdit->SetSelection(aSel);
1454 }
1455
1456 if(aSel.Min()!=aString.Len())
1457 {
1458 xub_StrLen nPos=(xub_StrLen)aSel.Min();
1459
1460 nFStart=GetFunctionPos(nPos - 1);
1461
1462 if(nFStart<nPos)
1463 {
1464 xub_StrLen nPos1=m_aFormulaHelper.GetFunctionEnd(aString,nFStart);
1465
1466 if(nPos1>nPos || nPos1==STRING_NOTFOUND)
1467 {
1468 EditThisFunc(nFStart);
1469 }
1470 else
1471 {
1472 xub_StrLen n=nPos;
1473 short nCount=1;
1474 while(n>0)
1475 {
1476 if(aString.GetChar(n)==')')
1477 nCount++;
1478 else if(aString.GetChar(n)=='(')
1479 nCount--;
1480 if(nCount==0) break;
1481 n--;
1482 }
1483 if(nCount==0)
1484 {
1485 nFStart=m_aFormulaHelper.GetFunctionStart(aString,n,sal_True);
1486 EditThisFunc(nFStart);
1487 }
1488 else
1489 {
1490 ClearAllParas();
1491 }
1492 }
1493 }
1494 else
1495 {
1496 ClearAllParas();
1497 }
1498 }
1499 m_pHelper->setSelection((xub_StrLen)aSel.Min(),(xub_StrLen)aSel.Max());
1500
1501 bEditFlag=sal_False;
1502 return 0;
1503 }
1504
UpdateSelection()1505 void FormulaDlg_Impl::UpdateSelection()
1506 {
1507 m_pHelper->setSelection((xub_StrLen)aFuncSel.Min(),(xub_StrLen)aFuncSel.Max());
1508 m_pHelper->setCurrentFormula( pFuncDesc->getFormula( m_aArguments ) );
1509 pMEdit->SetText(m_pHelper->getCurrentFormula());
1510 xub_StrLen PrivStart, PrivEnd;
1511 m_pHelper->getSelection( PrivStart, PrivEnd);
1512 aFuncSel.Min()=PrivStart;
1513 aFuncSel.Max()=PrivEnd;
1514
1515 nArgs = pFuncDesc->getSuppressedArgumentCount();
1516
1517 String aFormula=pMEdit->GetText();
1518 sal_Int32 nArgPos=m_aFormulaHelper.GetArgStart( aFormula,PrivStart,0);
1519
1520 sal_uInt16 nPos=pParaWin->GetActiveLine();
1521
1522 for(sal_uInt16 i=0;i<nPos;i++)
1523 {
1524 nArgPos += (m_aArguments[i].getLength() + 1);
1525 }
1526 sal_Int32 nLength= m_aArguments[nPos].getLength();
1527
1528 Selection aSel(nArgPos,nArgPos+nLength);
1529 m_pHelper->setSelection((sal_uInt16)nArgPos,(sal_uInt16)(nArgPos+nLength));
1530 pMEdit->SetSelection(aSel);
1531 aMEFormula.UpdateOldSel();
1532 }
RefInputStartBefore(RefEdit * pEdit,RefButton * pButton)1533 ::std::pair<RefButton*,RefEdit*> FormulaDlg_Impl::RefInputStartBefore( RefEdit* pEdit, RefButton* pButton )
1534 {
1535 aEdRef.Show();
1536 pTheRefEdit = pEdit;
1537 pTheRefButton = pButton;
1538
1539 if( pTheRefEdit )
1540 {
1541 aEdRef.SetRefString( pTheRefEdit->GetText() );
1542 aEdRef.SetSelection( pTheRefEdit->GetSelection() );
1543 aEdRef.SetHelpId( pTheRefEdit->GetHelpId() );
1544 aEdRef.SetUniqueId( pTheRefEdit->GetUniqueId() );
1545 }
1546
1547 aRefBtn.Show( pButton != NULL );
1548
1549 //m_pHelper->RefInputStart( &aEdRef, pButton ? &aRefBtn : NULL );
1550 ::std::pair<RefButton*,RefEdit*> aPair;
1551 aPair.first = pButton ? &aRefBtn : NULL;
1552 aPair.second = &aEdRef;
1553 return aPair;
1554 }
RefInputStartAfter(RefEdit *,RefButton *)1555 void FormulaDlg_Impl::RefInputStartAfter( RefEdit* /*pEdit*/, RefButton* /*pButton*/ )
1556 {
1557 aRefBtn.SetEndImage();
1558
1559 if( pTheRefEdit )
1560 {
1561 String aStr = aTitle2;
1562 aStr += ' ';
1563 aStr += aFtEditName.GetText();
1564 aStr.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "( " ) );
1565 if( pParaWin->GetActiveLine() > 0 )
1566 aStr.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "...; " ) );
1567 aStr += pParaWin->GetActiveArgName();
1568 if( pParaWin->GetActiveLine() + 1 < nArgs )
1569 aStr.AppendAscii(RTL_CONSTASCII_STRINGPARAM( "; ..." ));
1570 aStr.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " )" ) );
1571
1572 m_pParent->SetText( MnemonicGenerator::EraseAllMnemonicChars( aStr ) );
1573 }
1574 }
RefInputDoneAfter(sal_Bool bForced)1575 void FormulaDlg_Impl::RefInputDoneAfter( sal_Bool bForced )
1576 {
1577 aRefBtn.SetStartImage();
1578 if( bForced || !aRefBtn.IsVisible() )
1579 {
1580 aEdRef.Hide();
1581 aRefBtn.Hide();
1582 if( pTheRefEdit )
1583 {
1584 pTheRefEdit->SetRefString( aEdRef.GetText() );
1585 pTheRefEdit->GrabFocus();
1586
1587 if( pTheRefButton )
1588 pTheRefButton->SetStartImage();
1589
1590 sal_uInt16 nPrivActiv = pParaWin->GetActiveLine();
1591 pParaWin->SetArgument( nPrivActiv, aEdRef.GetText() );
1592 ModifyHdl( pParaWin );
1593 pTheRefEdit = NULL;
1594 }
1595 m_pParent->SetText( aTitle1 );
1596 }
1597 }
GetCurrRefEdit()1598 RefEdit* FormulaDlg_Impl::GetCurrRefEdit()
1599 {
1600 return aEdRef.IsVisible() ? &aEdRef : pParaWin->GetActiveEdit();
1601 }
Update()1602 void FormulaDlg_Impl::Update()
1603 {
1604 FormEditData* pData = m_pHelper->getFormEditData();
1605 const String sExpression = pMEdit->GetText();
1606 aOldFormula = String();
1607 UpdateTokenArray(sExpression);
1608 FormulaCursorHdl(&aMEFormula);
1609 CalcStruct(sExpression);
1610 if(pData->GetMode() == FORMULA_FORMDLG_FORMULA)
1611 aTabCtrl.SetCurPageId(TP_FUNCTION);
1612 else
1613 aTabCtrl.SetCurPageId(TP_STRUCT);
1614 aBtnMatrix.Check(pData->GetMatrixFlag());
1615 /*aTimer.SetTimeout(200);
1616 aTimer.SetTimeoutHdl(LINK( this, FormulaDlg_Impl, UpdateFocusHdl));
1617 aTimer.Start();*/
1618 }
Update(const String & _sExp)1619 void FormulaDlg_Impl::Update(const String& _sExp)
1620 {
1621 CalcStruct(_sExp);
1622 FillDialog();
1623 //aBtnForward.Enable(sal_True); //@New
1624 FuncSelHdl(NULL);
1625 }
SetMeText(const String & _sText)1626 void FormulaDlg_Impl::SetMeText(const String& _sText)
1627 {
1628 FormEditData* pData = m_pHelper->getFormEditData();
1629 pMEdit->SetText(_sText);
1630 pMEdit->SetSelection( pData->GetSelection());
1631 aMEFormula.UpdateOldSel();
1632 }
SetMeText(const String & _sText,xub_StrLen PrivStart,xub_StrLen PrivEnd,sal_Bool bMatrix,sal_Bool _bSelect,sal_Bool _bUpdate)1633 FormulaDlgMode FormulaDlg_Impl::SetMeText(const String& _sText,xub_StrLen PrivStart, xub_StrLen PrivEnd,sal_Bool bMatrix,sal_Bool _bSelect,sal_Bool _bUpdate)
1634 {
1635 FormulaDlgMode eMode = FORMULA_FORMDLG_FORMULA;
1636 if(!bEditFlag)
1637 pMEdit->SetText(_sText);
1638
1639 if ( _bSelect || !bEditFlag )
1640 pMEdit->SetSelection( Selection(PrivStart, PrivEnd));
1641 if ( _bUpdate )
1642 {
1643 aMEFormula.UpdateOldSel();
1644 pMEdit->Invalidate();
1645 m_pHelper->showReference(pMEdit->GetSelected());
1646 eMode = FORMULA_FORMDLG_EDIT;
1647
1648 aBtnMatrix.Check( bMatrix );
1649 } // if ( _bUpdate )
1650 return eMode;
1651 }
CheckMatrix(String & aFormula)1652 sal_Bool FormulaDlg_Impl::CheckMatrix(String& aFormula)
1653 {
1654 pMEdit->GrabFocus();
1655 xub_StrLen nLen = aFormula.Len();
1656 sal_Bool bMatrix = nLen > 3 // Matrix-Formel ?
1657 && aFormula.GetChar(0) == '{'
1658 && aFormula.GetChar(1) == '='
1659 && aFormula.GetChar(nLen-1) == '}';
1660 if ( bMatrix )
1661 {
1662 aFormula.Erase( 0, 1 );
1663 aFormula.Erase( aFormula.Len()-1, 1);
1664 aBtnMatrix.Check( bMatrix );
1665 aBtnMatrix.Disable();
1666 } // if ( bMatrix )
1667
1668 aTabCtrl.SetCurPageId(TP_STRUCT);
1669 return bMatrix;
1670 }
IMPL_LINK(FormulaDlg_Impl,StructSelHdl,StructPage *,pStruP)1671 IMPL_LINK( FormulaDlg_Impl, StructSelHdl, StructPage*, pStruP )
1672 {
1673 bStructUpdate=sal_False;
1674 if(pStructPage->IsVisible()) aBtnForward.Enable(sal_False); //@New
1675
1676 if(pStructPage==pStruP)
1677 {
1678 /// TODO
1679 //ScToken* pSelToken = pStructPage->GetSelectedToken();
1680 // ScToken* pOrigToken = ((pSelToken && pSelToken->GetType() == svFAP) ?
1681 // pSelToken->GetFAPOrigToken() : pSelToken);
1682 //xub_StrLen nTokPos=1;
1683
1684 //if(pScTokA!=NULL)
1685 //{
1686 // ScToken* pToken = pScTokA->First();
1687
1688 // while(pToken!=NULL)
1689 // {
1690 // String aString;
1691 // if ( pToken == pOrigToken )
1692 // break;
1693 // pComp->CreateStringFromToken( aString,pToken);
1694 // nTokPos = sal::static_int_cast<xub_StrLen>( nTokPos + aString.Len() );
1695 // pToken=pScTokA->Next();
1696 // }
1697 // EditThisFunc(nTokPos);
1698 //}
1699
1700 //if( pOrigToken )
1701 //{
1702 // String aStr;
1703 // pComp->CreateStringFromToken( aStr, pOrigToken );
1704 // String aEntryTxt=pStructPage->GetSelectedEntryText();
1705
1706 // if(aEntryTxt!=aStr)
1707 // ShowReference(aEntryTxt);
1708 //}
1709
1710 }
1711 bStructUpdate=sal_True;
1712 return 0;
1713 }
IMPL_LINK(FormulaDlg_Impl,MatrixHdl,CheckBox *,EMPTYARG)1714 IMPL_LINK( FormulaDlg_Impl, MatrixHdl, CheckBox *, EMPTYARG )
1715 {
1716 bUserMatrixFlag=sal_True;
1717 return 0;
1718 }
1719
IMPL_LINK(FormulaDlg_Impl,FuncSelHdl,FuncPage *,EMPTYARG)1720 IMPL_LINK( FormulaDlg_Impl, FuncSelHdl, FuncPage*, EMPTYARG )
1721 {
1722 sal_uInt16 nCat = pFuncPage->GetCategory();
1723 if ( nCat == LISTBOX_ENTRY_NOTFOUND ) nCat = 0;
1724 sal_uInt16 nFunc = pFuncPage->GetFunction();
1725 if ( nFunc == LISTBOX_ENTRY_NOTFOUND ) nFunc = 0;
1726
1727 if ( (pFuncPage->GetFunctionEntryCount() > 0)
1728 && (pFuncPage->GetFunction() != LISTBOX_ENTRY_NOTFOUND) )
1729 {
1730 const IFunctionDescription* pDesc =pFuncPage->GetFuncDesc( pFuncPage->GetFunction() );
1731
1732 if(pDesc!=pFuncDesc) aBtnForward.Enable(sal_True); //new
1733
1734 if (pDesc)
1735 {
1736 pDesc->initArgumentInfo(); // full argument info is needed
1737
1738 String aSig = pDesc->getSignature();
1739 aFtHeadLine.SetText( pDesc->getFunctionName() );
1740 aFtFuncName.SetText( aSig );
1741 aFtFuncDesc.SetText( pDesc->getDescription() );
1742 }
1743 }
1744 else
1745 {
1746 aFtHeadLine.SetText( String() );
1747 aFtFuncName.SetText( String() );
1748 aFtFuncDesc.SetText( String() );
1749 }
1750 return 0;
1751 }
1752
UpdateParaWin(const Selection & _rSelection,const String & _sRefStr)1753 void FormulaDlg_Impl::UpdateParaWin(const Selection& _rSelection,const String& _sRefStr)
1754 {
1755 Selection theSel = _rSelection;
1756 aEdRef.ReplaceSelected( _sRefStr );
1757 theSel.Max() = theSel.Min() + _sRefStr.Len();
1758 aEdRef.SetSelection( theSel );
1759
1760 //-------------------------------------
1761 // Manuelles Update der Ergebnisfelder:
1762 //-------------------------------------
1763 sal_uInt16 nPrivActiv = pParaWin->GetActiveLine();
1764 pParaWin->SetArgument(nPrivActiv,aEdRef.GetText());
1765 pParaWin->UpdateParas();
1766
1767 Edit* pEd = GetCurrRefEdit();
1768 if( pEd != NULL )
1769 pEd->SetSelection( theSel );
1770
1771 pParaWin->SetRefMode(sal_False);
1772 }
UpdateParaWin(Selection & _rSelection)1773 sal_Bool FormulaDlg_Impl::UpdateParaWin(Selection& _rSelection)
1774 {
1775 pParaWin->SetRefMode(sal_True);
1776
1777 String aStrEd;
1778 Edit* pEd = GetCurrRefEdit();
1779 if(pEd!=NULL && pTheRefEdit==NULL)
1780 {
1781 _rSelection=pEd->GetSelection();
1782 _rSelection.Justify();
1783 aStrEd=pEd->GetText();
1784 aEdRef.SetRefString(aStrEd);
1785 aEdRef.SetSelection( _rSelection );
1786 }
1787 else
1788 {
1789 _rSelection=aEdRef.GetSelection();
1790 _rSelection.Justify();
1791 aStrEd= aEdRef.GetText();
1792 }
1793 return pTheRefEdit == NULL;
1794 }
FindFocusWin(Window * pWin)1795 rtl::OString FormulaDlg_Impl::FindFocusWin(Window *pWin)
1796 {
1797 rtl::OString aUniqueId;
1798 if(pWin->HasFocus())
1799 {
1800 aUniqueId=pWin->GetUniqueId();
1801 if(aUniqueId.getLength()==0)
1802 {
1803 Window* pParent=pWin->GetParent();
1804 while(pParent!=NULL)
1805 {
1806 aUniqueId=pParent->GetUniqueId();
1807
1808 if(aUniqueId.getLength()!=0) break;
1809
1810 pParent=pParent->GetParent();
1811 }
1812 }
1813 }
1814 else
1815 {
1816 sal_uInt16 nCount=pWin->GetChildCount();
1817
1818 for(sal_uInt16 i=0;i<nCount;i++)
1819 {
1820 Window* pChild=pWin->GetChild(i);
1821 aUniqueId=FindFocusWin(pChild);
1822 if(aUniqueId.getLength()>0) break;
1823 }
1824 }
1825 return aUniqueId;
1826 }
1827
SetEdSelection()1828 void FormulaDlg_Impl::SetEdSelection()
1829 {
1830 Edit* pEd = GetCurrRefEdit()/*aScParaWin.GetActiveEdit()*/;
1831 if( pEd )
1832 {
1833 Selection theSel = aEdRef.GetSelection();
1834 // Edit may have the focus -> call ModifyHdl in addition
1835 // to what's happening in GetFocus
1836 pEd->GetModifyHdl().Call(pEd);
1837 pEd->GrabFocus();
1838 pEd->SetSelection(theSel);
1839 } // if( pEd )
1840 }
1841 // -----------------------------------------------------------------------------
GetFormulaHelper() const1842 const FormulaHelper& FormulaDlg_Impl::GetFormulaHelper() const
1843 {
1844 return m_aFormulaHelper;
1845 }
1846 //============================================================================
FormulaModalDialog(Window * pParent,bool _bSupportFunctionResult,bool _bSupportResult,bool _bSupportMatrix,IFormulaEditorHelper * _pHelper,IFunctionManager * _pFunctionMgr,IControlReferenceHandler * _pDlg)1847 FormulaModalDialog::FormulaModalDialog( Window* pParent
1848 , bool _bSupportFunctionResult
1849 , bool _bSupportResult
1850 , bool _bSupportMatrix
1851 , IFormulaEditorHelper* _pHelper
1852 , IFunctionManager* _pFunctionMgr
1853 , IControlReferenceHandler* _pDlg ) :
1854 ModalDialog( pParent, ModuleRes(RID_FORMULADLG_FORMULA_MODAL) ),
1855 m_pImpl( new FormulaDlg_Impl(this,_bSupportFunctionResult
1856 , _bSupportResult
1857 , _bSupportMatrix
1858 ,_pHelper,_pFunctionMgr,_pDlg))
1859 {
1860 FreeResource();
1861 SetText(m_pImpl->aTitle1);
1862 }
~FormulaModalDialog()1863 FormulaModalDialog::~FormulaModalDialog()
1864 {
1865 }
1866 // -----------------------------------------------------------------------------
Update(const String & _sExp)1867 void FormulaModalDialog::Update(const String& _sExp)
1868 {
1869 m_pImpl->Update(_sExp);
1870 }
1871
1872 // -----------------------------------------------------------------------------
SetMeText(const String & _sText)1873 void FormulaModalDialog::SetMeText(const String& _sText)
1874 {
1875 m_pImpl->SetMeText(_sText);
1876 }
1877
1878 // -----------------------------------------------------------------------------
SetMeText(const String & _sText,xub_StrLen PrivStart,xub_StrLen PrivEnd,sal_Bool bMatrix,sal_Bool _bSelect,sal_Bool _bUpdate)1879 FormulaDlgMode FormulaModalDialog::SetMeText(const String& _sText,xub_StrLen PrivStart, xub_StrLen PrivEnd,sal_Bool bMatrix,sal_Bool _bSelect,sal_Bool _bUpdate)
1880 {
1881 return m_pImpl->SetMeText(_sText,PrivStart, PrivEnd,bMatrix,_bSelect,_bUpdate);
1882 }
1883 // -----------------------------------------------------------------------------
CheckMatrix()1884 void FormulaModalDialog::CheckMatrix()
1885 {
1886 m_pImpl->aBtnMatrix.Check();
1887 }
1888 // -----------------------------------------------------------------------------
CheckMatrix(String & aFormula)1889 sal_Bool FormulaModalDialog::CheckMatrix(String& aFormula)
1890 {
1891 return m_pImpl->CheckMatrix(aFormula);
1892 }
1893 // -----------------------------------------------------------------------------
GetMeText() const1894 String FormulaModalDialog::GetMeText() const
1895 {
1896 return m_pImpl->pMEdit->GetText();
1897 }
1898 // -----------------------------------------------------------------------------
Update()1899 void FormulaModalDialog::Update()
1900 {
1901 m_pImpl->Update();
1902 }
1903 // -----------------------------------------------------------------------------
GetFormulaHelper() const1904 const FormulaHelper& FormulaModalDialog::GetFormulaHelper() const
1905 {
1906 return m_pImpl->GetFormulaHelper();
1907 }
1908 // -----------------------------------------------------------------------------
isUserMatrix() const1909 sal_Bool FormulaModalDialog::isUserMatrix() const
1910 {
1911 return m_pImpl->bUserMatrixFlag;
1912 }
DoEnter(sal_Bool _bOk)1913 void FormulaModalDialog::DoEnter(sal_Bool _bOk)
1914 {
1915 m_pImpl->DoEnter(_bOk);
1916 }
RefInputStartBefore(RefEdit * pEdit,RefButton * pButton)1917 ::std::pair<RefButton*,RefEdit*> FormulaModalDialog::RefInputStartBefore( RefEdit* pEdit, RefButton* pButton )
1918 {
1919 return m_pImpl->RefInputStartBefore( pEdit, pButton );
1920 }
RefInputStartAfter(RefEdit * pEdit,RefButton * pButton)1921 void FormulaModalDialog::RefInputStartAfter( RefEdit* pEdit, RefButton* pButton )
1922 {
1923 m_pImpl->RefInputStartAfter( pEdit, pButton );
1924 }
RefInputDoneAfter(sal_Bool bForced)1925 void FormulaModalDialog::RefInputDoneAfter( sal_Bool bForced )
1926 {
1927 m_pImpl->RefInputDoneAfter( bForced );
1928 }
1929
FindFocusWin(Window * pWin)1930 rtl::OString FormulaModalDialog::FindFocusWin(Window *pWin)
1931 {
1932 return m_pImpl->FindFocusWin( pWin );
1933 }
1934
SetFocusWin(Window * pWin,const rtl::OString & nUniqueId)1935 void FormulaModalDialog::SetFocusWin(Window *pWin,const rtl::OString& nUniqueId)
1936 {
1937 if(pWin->GetUniqueId()==nUniqueId)
1938 {
1939 pWin->GrabFocus();
1940 }
1941 else
1942 {
1943 sal_uInt16 nCount=pWin->GetChildCount();
1944
1945 for(sal_uInt16 i=0;i<nCount;i++)
1946 {
1947 Window* pChild=pWin->GetChild(i);
1948 SetFocusWin(pChild,nUniqueId);
1949 }
1950 }
1951 }
1952
1953
PreNotify(NotifyEvent & rNEvt)1954 long FormulaModalDialog::PreNotify( NotifyEvent& rNEvt )
1955 {
1956 m_pImpl->PreNotify( rNEvt );
1957
1958 return ModalDialog::PreNotify(rNEvt);
1959 }
1960
HighlightFunctionParas(const String & aFormula)1961 void FormulaModalDialog::HighlightFunctionParas(const String& aFormula)
1962 {
1963 m_pImpl->m_pHelper->showReference(aFormula);
1964 }
1965
disableOk()1966 void FormulaModalDialog::disableOk()
1967 {
1968 m_pImpl->aBtnEnd.Disable();
1969 }
1970 // -----------------------------------------------------------------------------
getCurrentFunctionDescription() const1971 const IFunctionDescription* FormulaModalDialog::getCurrentFunctionDescription() const
1972 {
1973 OSL_VERIFY(!m_pImpl->pFuncDesc || m_pImpl->pFuncDesc->getSuppressedArgumentCount() == m_pImpl->nArgs);
1974 return m_pImpl->pFuncDesc;
1975 }
1976 // -----------------------------------------------------------------------------
UpdateParaWin(const Selection & _rSelection,const String & _sRefStr)1977 void FormulaModalDialog::UpdateParaWin(const Selection& _rSelection,const String& _sRefStr)
1978 {
1979 m_pImpl->UpdateParaWin(_rSelection,_sRefStr);
1980 }
UpdateParaWin(Selection & _rSelection)1981 sal_Bool FormulaModalDialog::UpdateParaWin(Selection& _rSelection)
1982 {
1983 return m_pImpl->UpdateParaWin(_rSelection);
1984 }
1985 // -----------------------------------------------------------------------------
GetActiveEdit()1986 RefEdit* FormulaModalDialog::GetActiveEdit()
1987 {
1988 return m_pImpl->pParaWin->GetActiveEdit();
1989 }
1990 // -----------------------------------------------------------------------------
SetEdSelection()1991 void FormulaModalDialog::SetEdSelection()
1992 {
1993 m_pImpl->SetEdSelection();
1994 }
1995
1996 // --------------------------------------------------------------------------
1997 // Initialisierung / gemeinsaME Funktionen fuer Dialog
1998 // --------------------------------------------------------------------------
FormulaDlg(SfxBindings * pB,SfxChildWindow * pCW,Window * pParent,bool _bSupportFunctionResult,bool _bSupportResult,bool _bSupportMatrix,IFormulaEditorHelper * _pHelper,IFunctionManager * _pFunctionMgr,IControlReferenceHandler * _pDlg)1999 FormulaDlg::FormulaDlg( SfxBindings* pB, SfxChildWindow* pCW,
2000 Window* pParent
2001 , bool _bSupportFunctionResult
2002 , bool _bSupportResult
2003 , bool _bSupportMatrix
2004 , IFormulaEditorHelper* _pHelper,IFunctionManager* _pFunctionMgr,IControlReferenceHandler* _pDlg ) :
2005 SfxModelessDialog( pB, pCW, pParent, ModuleRes(RID_FORMULADLG_FORMULA) ),
2006 m_pImpl( new FormulaDlg_Impl(this, _bSupportFunctionResult
2007 , _bSupportResult
2008 , _bSupportMatrix
2009 ,_pHelper,_pFunctionMgr,_pDlg))
2010 {
2011 FreeResource();
2012 if(!GetHelpId().getLength()) //Hack, da im SfxModelessDialog die HelpId
2013 SetHelpId(GetUniqueId()); //fuer einen ModelessDialog entfernt und
2014 //in eine UniqueId gewandelt wird, machen
2015 //wir das an dieser Stelle rueckgaengig.
2016 SetText(m_pImpl->aTitle1);
2017 }
2018
~FormulaDlg()2019 FormulaDlg::~FormulaDlg()
2020 {
2021 }
2022 // -----------------------------------------------------------------------------
Update(const String & _sExp)2023 void FormulaDlg::Update(const String& _sExp)
2024 {
2025 m_pImpl->Update(_sExp);
2026 }
2027
2028 // -----------------------------------------------------------------------------
SetMeText(const String & _sText)2029 void FormulaDlg::SetMeText(const String& _sText)
2030 {
2031 m_pImpl->SetMeText(_sText);
2032 }
2033
2034 // -----------------------------------------------------------------------------
SetMeText(const String & _sText,xub_StrLen PrivStart,xub_StrLen PrivEnd,sal_Bool bMatrix,sal_Bool _bSelect,sal_Bool _bUpdate)2035 FormulaDlgMode FormulaDlg::SetMeText(const String& _sText,xub_StrLen PrivStart, xub_StrLen PrivEnd,sal_Bool bMatrix,sal_Bool _bSelect,sal_Bool _bUpdate)
2036 {
2037 return m_pImpl->SetMeText(_sText,PrivStart, PrivEnd,bMatrix,_bSelect,_bUpdate);
2038 }
2039 // -----------------------------------------------------------------------------
CheckMatrix()2040 void FormulaDlg::CheckMatrix()
2041 {
2042 m_pImpl->aBtnMatrix.Check();
2043 }
2044 // -----------------------------------------------------------------------------
CheckMatrix(String & aFormula)2045 sal_Bool FormulaDlg::CheckMatrix(String& aFormula)
2046 {
2047 return m_pImpl->CheckMatrix(aFormula);
2048 }
2049 // -----------------------------------------------------------------------------
GetMeText() const2050 String FormulaDlg::GetMeText() const
2051 {
2052 return m_pImpl->pMEdit->GetText();
2053 }
2054 // -----------------------------------------------------------------------------
Update()2055 void FormulaDlg::Update()
2056 {
2057 m_pImpl->Update();
2058 m_pImpl->aTimer.SetTimeout(200);
2059 m_pImpl->aTimer.SetTimeoutHdl(LINK( this, FormulaDlg, UpdateFocusHdl));
2060 m_pImpl->aTimer.Start();
2061 }
2062
2063 // -----------------------------------------------------------------------------
isUserMatrix() const2064 sal_Bool FormulaDlg::isUserMatrix() const
2065 {
2066 return m_pImpl->bUserMatrixFlag;
2067 }
DoEnter(sal_Bool _bOk)2068 void FormulaDlg::DoEnter(sal_Bool _bOk)
2069 {
2070 m_pImpl->DoEnter(_bOk);
2071 }
RefInputStartBefore(RefEdit * pEdit,RefButton * pButton)2072 ::std::pair<RefButton*,RefEdit*> FormulaDlg::RefInputStartBefore( RefEdit* pEdit, RefButton* pButton )
2073 {
2074 return m_pImpl->RefInputStartBefore( pEdit, pButton );
2075 }
RefInputStartAfter(RefEdit * pEdit,RefButton * pButton)2076 void FormulaDlg::RefInputStartAfter( RefEdit* pEdit, RefButton* pButton )
2077 {
2078 m_pImpl->RefInputStartAfter( pEdit, pButton );
2079 }
RefInputDoneAfter(sal_Bool bForced)2080 void FormulaDlg::RefInputDoneAfter( sal_Bool bForced )
2081 {
2082 m_pImpl->RefInputDoneAfter( bForced );
2083 }
2084
FindFocusWin(Window * pWin)2085 rtl::OString FormulaDlg::FindFocusWin(Window *pWin)
2086 {
2087 return m_pImpl->FindFocusWin( pWin );
2088 }
2089
SetFocusWin(Window * pWin,const rtl::OString & nUniqueId)2090 void FormulaDlg::SetFocusWin(Window *pWin,const rtl::OString& nUniqueId)
2091 {
2092 if(pWin->GetUniqueId()==nUniqueId)
2093 {
2094 pWin->GrabFocus();
2095 }
2096 else
2097 {
2098 sal_uInt16 nCount=pWin->GetChildCount();
2099
2100 for(sal_uInt16 i=0;i<nCount;i++)
2101 {
2102 Window* pChild=pWin->GetChild(i);
2103 SetFocusWin(pChild,nUniqueId);
2104 }
2105 }
2106 }
2107
2108
PreNotify(NotifyEvent & rNEvt)2109 long FormulaDlg::PreNotify( NotifyEvent& rNEvt )
2110 {
2111 m_pImpl->PreNotify( rNEvt );
2112 return SfxModelessDialog::PreNotify(rNEvt);
2113 }
2114
HighlightFunctionParas(const String & aFormula)2115 void FormulaDlg::HighlightFunctionParas(const String& aFormula)
2116 {
2117 m_pImpl->m_pHelper->showReference(aFormula);
2118 }
2119
disableOk()2120 void FormulaDlg::disableOk()
2121 {
2122 m_pImpl->aBtnEnd.Disable();
2123 }
2124 // -----------------------------------------------------------------------------
getCurrentFunctionDescription() const2125 const IFunctionDescription* FormulaDlg::getCurrentFunctionDescription() const
2126 {
2127 OSL_VERIFY(!m_pImpl->pFuncDesc || m_pImpl->pFuncDesc->getSuppressedArgumentCount() == m_pImpl->nArgs);
2128 return m_pImpl->pFuncDesc;
2129 }
2130 // -----------------------------------------------------------------------------
UpdateParaWin(const Selection & _rSelection,const String & _sRefStr)2131 void FormulaDlg::UpdateParaWin(const Selection& _rSelection,const String& _sRefStr)
2132 {
2133 m_pImpl->UpdateParaWin(_rSelection,_sRefStr);
2134 }
UpdateParaWin(Selection & _rSelection)2135 sal_Bool FormulaDlg::UpdateParaWin(Selection& _rSelection)
2136 {
2137 return m_pImpl->UpdateParaWin(_rSelection);
2138 }
2139 // -----------------------------------------------------------------------------
GetActiveEdit()2140 RefEdit* FormulaDlg::GetActiveEdit()
2141 {
2142 return m_pImpl->pParaWin->GetActiveEdit();
2143 }
2144 // -----------------------------------------------------------------------------
GetFormulaHelper() const2145 const FormulaHelper& FormulaDlg::GetFormulaHelper() const
2146 {
2147 return m_pImpl->GetFormulaHelper();
2148 }
2149 // -----------------------------------------------------------------------------
SetEdSelection()2150 void FormulaDlg::SetEdSelection()
2151 {
2152 m_pImpl->SetEdSelection();
2153 }
IMPL_LINK(FormulaDlg,UpdateFocusHdl,Timer *,EMPTYARG)2154 IMPL_LINK( FormulaDlg, UpdateFocusHdl, Timer*, EMPTYARG )
2155 {
2156 FormEditData* pData = m_pImpl->m_pHelper->getFormEditData();
2157
2158 if (pData) // wird nicht ueber Close zerstoert;
2159 {
2160 m_pImpl->m_pHelper->setReferenceInput(pData);
2161 rtl::OString nUniqueId(pData->GetUniqueId());
2162 SetFocusWin(this,nUniqueId);
2163 }
2164 return 0;
2165 }
2166
2167 // -----------------------------------------------------------------------------
2168 // -----------------------------------------------------------------------------
SaveValues()2169 void FormEditData::SaveValues()
2170 {
2171 FormEditData* pTemp = new FormEditData(*this);
2172
2173 Reset();
2174 pParent = pTemp;
2175 }
2176 // -----------------------------------------------------------------------------
Reset()2177 void FormEditData::Reset()
2178 {
2179 pParent = NULL;
2180 nMode = 0;
2181 nFStart = 0;
2182 nCatSel = 1; //! oder 0 (zuletzt benutzte)
2183 nFuncSel = 0;
2184 nOffset = 0;
2185 nEdFocus = 0;
2186 bMatrix =sal_False;
2187 aUniqueId=rtl::OString();
2188 aSelection.Min()=0;
2189 aSelection.Max()=0;
2190 aUndoStr.Erase();
2191 }
2192 // -----------------------------------------------------------------------------
RestoreValues()2193 void FormEditData::RestoreValues()
2194 {
2195 FormEditData* pTemp = pParent;
2196 DBG_ASSERT(pTemp,"RestoreValues ohne Parent");
2197 if (pTemp)
2198 {
2199 *this = *pTemp;
2200 pTemp->pParent = NULL; // sonst wird der auch geloescht!
2201 delete pTemp;
2202 }
2203 }
2204 // -----------------------------------------------------------------------------
operator =(const FormEditData & r)2205 const FormEditData& FormEditData::operator=( const FormEditData& r )
2206 {
2207 pParent = r.pParent;
2208 nMode = r.nMode;
2209 nFStart = r.nFStart;
2210 nCatSel = r.nCatSel;
2211 nFuncSel = r.nFuncSel;
2212 nOffset = r.nOffset;
2213 nEdFocus = r.nEdFocus;
2214 aUndoStr = r.aUndoStr;
2215 bMatrix = r.bMatrix ;
2216 aUniqueId = r.aUniqueId;
2217 aSelection = r.aSelection;
2218 return *this;
2219 }
2220 // -----------------------------------------------------------------------------
FormEditData()2221 FormEditData::FormEditData()
2222 {
2223 Reset();
2224 }
2225
~FormEditData()2226 FormEditData::~FormEditData()
2227 {
2228 delete pParent;
2229 }
2230
FormEditData(const FormEditData & r)2231 FormEditData::FormEditData( const FormEditData& r )
2232 {
2233 *this = r;
2234 }
2235
2236 // -----------------------------------------------------------------------------
2237 } // formula
2238 // -----------------------------------------------------------------------------
2239