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 "rangelst.hxx"
21 #include "scitems.hxx"
22 #include <sfx2/dispatch.hxx>
23 #include <svl/zforlist.hxx>
24 #include <vcl/msgbox.hxx>
26 #include "uiitems.hxx"
27 #include "reffact.hxx"
28 #include "document.hxx"
29 #include "scresid.hxx"
30 #include "globstr.hrc"
34 #include "solvrdlg.hxx"
39 void lclErrorDialog( Window
* pParent
, OUString aString
)
41 ErrorBox( pParent
, WinBits( WB_OK
| WB_DEF_OK
), aString
).Execute();
45 ScSolverDlg::ScSolverDlg( SfxBindings
* pB
, SfxChildWindow
* pCW
, Window
* pParent
,
46 ScDocument
* pDocument
,
47 ScAddress aCursorPos
)
49 : ScAnyRefDlg(pB
, pCW
, pParent
, "GoalSeekDialog", "modules/scalc/ui/goalseekdlg.ui")
50 , theFormulaCell(aCursorPos
)
51 , theVariableCell(aCursorPos
)
53 , nCurTab(aCursorPos
.Tab())
55 , bDlgLostFocus(false)
56 , errMsgInvalidVar(ScGlobal::GetRscString(STR_INVALIDVAR
))
57 , errMsgInvalidForm(ScGlobal::GetRscString(STR_INVALIDFORM
))
58 , errMsgNoFormula(ScGlobal::GetRscString(STR_NOFORMULA
))
59 , errMsgInvalidVal(ScGlobal::GetRscString(STR_INVALIDVAL
))
61 get(m_pFtFormulaCell
, "formulatext");
62 get(m_pEdFormulaCell
, "formulaedit");
63 m_pEdFormulaCell
->SetReferences(this, m_pFtFormulaCell
);
64 get(m_pRBFormulaCell
, "formulabutton");
65 m_pRBFormulaCell
->SetReferences(this, m_pEdFormulaCell
),
66 get(m_pEdTargetVal
, "target");
67 get(m_pFtVariableCell
, "vartext");
68 get(m_pEdVariableCell
, "varedit");
69 m_pEdVariableCell
->SetReferences(this, m_pFtVariableCell
);
70 get(m_pRBVariableCell
, "varbutton");
71 m_pRBVariableCell
->SetReferences(this, m_pEdVariableCell
);
73 get(m_pBtnCancel
, "cancel");
77 //----------------------------------------------------------------------------
79 ScSolverDlg::~ScSolverDlg()
83 //----------------------------------------------------------------------------
85 void ScSolverDlg::Init()
87 m_pBtnOk
->SetClickHdl( LINK( this, ScSolverDlg
, BtnHdl
) );
88 m_pBtnCancel
->SetClickHdl( LINK( this, ScSolverDlg
, BtnHdl
) );
90 Link aLink
= LINK( this, ScSolverDlg
, GetFocusHdl
);
91 m_pEdFormulaCell
->SetGetFocusHdl( aLink
);
92 m_pRBFormulaCell
->SetGetFocusHdl( aLink
);
93 m_pEdVariableCell
->SetGetFocusHdl( aLink
);
94 m_pRBVariableCell
->SetGetFocusHdl( aLink
);
95 m_pEdTargetVal
->SetGetFocusHdl( aLink
);
97 aLink
= LINK( this, ScSolverDlg
, LoseFocusHdl
);
98 m_pEdFormulaCell
->SetLoseFocusHdl ( aLink
);
99 m_pRBFormulaCell
->SetLoseFocusHdl ( aLink
);
100 m_pEdVariableCell
->SetLoseFocusHdl ( aLink
);
101 m_pRBVariableCell
->SetLoseFocusHdl ( aLink
);
103 OUString
aStr(theFormulaCell
.Format(SCA_ABS
, NULL
, pDoc
->GetAddressConvention()));
105 m_pEdFormulaCell
->SetText( aStr
);
106 m_pEdFormulaCell
->GrabFocus();
107 pEdActive
= m_pEdFormulaCell
;
110 //----------------------------------------------------------------------------
112 sal_Bool
ScSolverDlg::Close()
114 return DoClose( ScSolverDlgWrapper::GetChildWindowId() );
117 //----------------------------------------------------------------------------
119 void ScSolverDlg::SetActive()
123 bDlgLostFocus
= false;
125 pEdActive
->GrabFocus();
134 //----------------------------------------------------------------------------
136 void ScSolverDlg::SetReference( const ScRange
& rRef
, ScDocument
* pDocP
)
140 if ( rRef
.aStart
!= rRef
.aEnd
)
141 RefInputStart(pEdActive
);
143 ScAddress aAdr
= rRef
.aStart
;
144 sal_uInt16 nFmt
= ( aAdr
.Tab() == nCurTab
)
148 OUString
aStr(aAdr
.Format(nFmt
, pDocP
, pDocP
->GetAddressConvention()));
149 pEdActive
->SetRefString( aStr
);
151 if ( pEdActive
== m_pEdFormulaCell
)
152 theFormulaCell
= aAdr
;
153 else if ( pEdActive
== m_pEdVariableCell
)
154 theVariableCell
= aAdr
;
158 //----------------------------------------------------------------------------
160 void ScSolverDlg::RaiseError( ScSolverErr eError
)
164 case SOLVERR_NOFORMULA
:
165 lclErrorDialog( this, errMsgNoFormula
);
166 m_pEdFormulaCell
->GrabFocus();
169 case SOLVERR_INVALID_FORMULA
:
170 lclErrorDialog( this, errMsgInvalidForm
);
171 m_pEdFormulaCell
->GrabFocus();
174 case SOLVERR_INVALID_VARIABLE
:
175 lclErrorDialog( this, errMsgInvalidVar
);
176 m_pEdVariableCell
->GrabFocus();
179 case SOLVERR_INVALID_TARGETVALUE
:
180 lclErrorDialog( this, errMsgInvalidVal
);
181 m_pEdTargetVal
->GrabFocus();
186 //----------------------------------------------------------------------------
188 sal_Bool
ScSolverDlg::IsRefInputMode() const
190 return pEdActive
!= NULL
;
193 //----------------------------------------------------------------------------
195 sal_Bool
ScSolverDlg::CheckTargetValue( const OUString
& rStrVal
)
200 return pDoc
->GetFormatTable()->IsNumberFormat( rStrVal
, n1
, n2
);
203 //----------------------------------------------------------------------------
206 IMPL_LINK( ScSolverDlg
, BtnHdl
, PushButton
*, pBtn
)
208 if (pBtn
== m_pBtnOk
)
210 theTargetValStr
= m_pEdTargetVal
->GetText();
213 // 1. enthalten die Strings korrekte Tabellenkoordinaten/def.Namen?
214 // 2. verweist die Formel-Koordinate wirklich auf eine Formelzelle?
215 // 3. wurde ein korrekter Zielwert eingegeben
217 const formula::FormulaGrammar::AddressConvention eConv
= pDoc
->GetAddressConvention();
218 sal_uInt16 nRes1
= theFormulaCell
.Parse( m_pEdFormulaCell
->GetText(), pDoc
, eConv
);
219 sal_uInt16 nRes2
= theVariableCell
.Parse( m_pEdVariableCell
->GetText(), pDoc
, eConv
);
221 if ( SCA_VALID
== ( nRes1
& SCA_VALID
) )
223 if ( SCA_VALID
== ( nRes2
& SCA_VALID
) )
225 if ( CheckTargetValue( theTargetValStr
) )
228 pDoc
->GetCellType( theFormulaCell
.Col(),
229 theFormulaCell
.Row(),
230 theFormulaCell
.Tab(),
233 if ( CELLTYPE_FORMULA
== eType
)
235 ScSolveParam
aOutParam( theFormulaCell
,
238 ScSolveItem
aOutItem( SCITEM_SOLVEDATA
, &aOutParam
);
240 SetDispatcherLock( false );
243 GetBindings().GetDispatcher()->Execute( SID_SOLVE
,
244 SFX_CALLMODE_SLOT
| SFX_CALLMODE_RECORD
,
248 else RaiseError( SOLVERR_NOFORMULA
);
250 else RaiseError( SOLVERR_INVALID_TARGETVALUE
);
252 else RaiseError( SOLVERR_INVALID_VARIABLE
);
254 else RaiseError( SOLVERR_INVALID_FORMULA
);
256 else if (pBtn
== m_pBtnCancel
)
264 //----------------------------------------------------------------------------
266 IMPL_LINK( ScSolverDlg
, GetFocusHdl
, Control
*, pCtrl
)
271 if( (pCtrl
== (Control
*)m_pEdFormulaCell
) || (pCtrl
== (Control
*)m_pRBFormulaCell
) )
272 pEdit
= pEdActive
= m_pEdFormulaCell
;
273 else if( (pCtrl
== (Control
*)m_pEdVariableCell
) || (pCtrl
== (Control
*)m_pRBVariableCell
) )
274 pEdit
= pEdActive
= m_pEdVariableCell
;
275 else if( pCtrl
== (Control
*)m_pEdTargetVal
)
276 pEdit
= m_pEdTargetVal
;
279 pEdit
->SetSelection( Selection( 0, SELECTION_MAX
) );
284 //----------------------------------------------------------------------------
286 IMPL_LINK_NOARG(ScSolverDlg
, LoseFocusHdl
)
288 bDlgLostFocus
= !IsActive();
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */